-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP32-Arduino-3dHall-Example.ino
539 lines (464 loc) · 16.4 KB
/
ESP32-Arduino-3dHall-Example.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
* Example code for reading an ALS31313 3D Hall Effect Sensor on ESP32 in Arduino environment.
* VintLabs 3dHall Module available at https://www.amazon.ca/dp/B0896VD73G
*
* Compile and run - I recommend using the serial plotter rather than the serial console for testing!
*
*
* Much of the code is taken from the example code provided by Allegro Microsystems:
* Example source code for an Arduino to show
* how to communicate with an Allegro ALS31300
*
* Written by K. Robert Bate, Allegro MicroSystems, LLC.
*
* ALS31300Demo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
// Redefine these if you're using non-standard pins
//#define SDA = 5
//#define SCL = 4
#include <Wire.h>
#include <math.h>
char xyzB[128] = "";
char hall_d[8] = "";
int xd = 0;
int yd = 0;
int zd = 0;
int rx = 0;
int ry = 0;
int rz = 0;
// Return values of endTransmission in the Wire library
#define kNOERROR 0
#define kDATATOOLONGERROR 1
#define kRECEIVEDNACKONADDRESSERROR 2
#define kRECEIVEDNACKONDATAERROR 3
#define kOTHERERROR 4
//#define M_PI 3.14159265358979323846
//#define M_TWOPI (M_PI * 2.0)
// led blinking support
bool ledState = false;
//int ledPin = 13;
unsigned long nextTime;
String ugh = String("");
int hall_mode = 0;
int deviceAddress = 0x60; // Address of the ALS31300
// SCL Pin = 19
// SDA Pin = 18
//
// setup
//
// Initializes the Wire library for I2C communications,
// Serial for displaying the results and error messages,
// the hardware and variables to blink the LED,
// and sets the ALS31300 into customer access mode.
//
void setup()
{
// Initialize the I2C communication library
Wire.begin(SDA, SCL);
Wire.setClock(1000000); // 1 MHz
// Initialize the serial port
Serial.begin(115200);
// If using a Arduino with USB built in, uncomment the next line,
// this allows the errors in Setup to be seen
// while (!Serial);
// Setup hardware and variables for code which blinks the LED
nextTime = millis();
//pinMode(ledPin, OUTPUT);
//digitalWrite(ledPin, LOW);
// Enter customer access mode on the ALS31300
uint16_t error = write(deviceAddress, 0x24, 0x2C413534);
if (error != kNOERROR)
{
Serial.print("Error while trying to enter customer access mode. error = ");
Serial.println(error);
}
Wire.requestFrom(0x02, 4);
uint32_t data = Wire.read() << 24;
data += Wire.read() << 16;
data += Wire.read() << 8;
data += Wire.read();
Serial.printf("%08x\n", data);
char a = bitRead(data,20);
char b = bitRead(data,19);
hall_mode |= a << 0;
hall_mode |= b << 1;
// set up the legend for plotter
Serial.println("x y z angleXY angleXZ angleYZ");
}
void loop()
{
readALS31300ADC(deviceAddress);
delay(100);
}
//
// readALS31300ADC
//
// Read the X, Y, Z values from Register 0x28 and 0x29
// eight times. No loop mode is used.
//
void readALS31300ADC(int busAddress)
{
uint32_t value0x27;
// Read the register the I2C loop mode is in
uint16_t error = read(busAddress, 0x27, value0x27);
if (error != kNOERROR)
{
Serial.print("Unable to read the ALS31300. error = ");
Serial.println(error);
}
// I2C loop mode is in bits 2 and 3 so mask them out
// and set them to the no loop mode
value0x27 = (value0x27 & 0xFFFFFFF3) | (0x0 << 2);
// Write the new values to the register the I2C loop mode is in
error = write(busAddress, 0x27, value0x27);
if (error != kNOERROR)
{
Serial.print("Unable to read the ALS31300. error = ");
Serial.println(error);
}
xd = 0;
yd = 0;
zd = 0;
for (int count = 0; count < 1; ++count)
{
// Write the address that is going to be read from the ALS31300
Wire.beginTransmission(busAddress);
Wire.write(0x28);
uint16_t error = Wire.endTransmission(false);
// The ALS31300 accepted the address
if (error == kNOERROR)
{
// Start the read and request 8 bytes
// which are the contents of register 0x28 and 0x29
Wire.requestFrom(busAddress, 8);
// Read the first 4 bytes which are the contents of register 0x28
uint32_t value0x28 = Wire.read() << 24;
value0x28 += Wire.read() << 16;
value0x28 += Wire.read() << 8;
value0x28 += Wire.read();
// Read the next 4 bytes which are the contents of register 0x29
uint32_t value0x29 = Wire.read() << 24;
value0x29 += Wire.read() << 16;
value0x29 += Wire.read() << 8;
value0x29 += Wire.read();
// Take the most significant byte of each axis from register 0x28 and combine it with the least
// significant 4 bits of each axis from register 0x29, then sign extend the 12th bit.
int x = SignExtendBitfield(((value0x28 >> 20) & 0x0FF0) | ((value0x29 >> 16) & 0x0F), 12);
int y = SignExtendBitfield(((value0x28 >> 12) & 0x0FF0) | ((value0x29 >> 12) & 0x0F), 12);
int z = SignExtendBitfield(((value0x28 >> 4) & 0x0FF0) | ((value0x29 >> 8) & 0x0F), 12);
xd += x;
yd += y;
zd += z;
// Display the values of x, y and z
//sprintf(xyzB,"%d x: %d y: %d z: %d", count, x,y,z);
//sprintf(xyzB,"%d ", count);
// Look at the datasheet for the sensitivity of the part used.
// In this case, full scale range is 500 gauss, other sensitivities
// are 1000 gauss and 2000 gauss.
// Sensitivity of 500 gauss = 4.0 lsb/g
// Sensitivity of 1000 gauss = 2.0 lsb/g
// Sensitivity of 2000 gauss = 1.0 lsb/g
float mx = (float)x / 4.0;
float my = (float)y / 4.0;
float mz = (float)z / 4.0;
/*
*
Serial.print("MX, MY, MZ = ");
Serial.print(mx);
Serial.print(", ");
Serial.print(my);
Serial.print(", ");
Serial.print(mz);
Serial.println(" Gauss");
*/
// Convert the X, Y and Z values into radians
float rx = (float)x / 4096.0 * M_TWOPI;
float ry = (float)y / 4096.0 * M_TWOPI;
float rz = (float)z / 4096.0 * M_TWOPI;
// Use a four quadrant Arc Tan to convert 2
// axis to an angle (which is in radians) then
// convert the angle from radians to degrees
// for display.
float angleXY = atan2f(ry, rx) * 180.0 / M_PI;
float angleXZ = atan2f(rz, rx) * 180.0 / M_PI;
float angleYZ = atan2f(rz, ry) * 180.0 / M_PI;
/*
*
Serial.print("angleXY, angleXZ, angleYZ = ");
Serial.print(angleXY);
Serial.print(", ");
Serial.print(angleXZ);
Serial.print(", ");
Serial.print(angleYZ);
Serial.println(" Degrees");
*/
//Serial.println("x y z mx my mz axy axz yz");
//Serial.printf("%3d %3d %3d %5.2f %5.2f %5.2f %11.6f %11.6f %11.6f\n", x, y, z, mx, my, mz, angleXY, angleXZ, angleYZ);
Serial.printf("%3d %3d %3d %11.6f %11.6f %11.6f\n", x, y, z, angleXY, angleXZ, angleYZ);
}
else
{
Serial.print("Unable to read the ALS31300. error = ");
Serial.println(error);
break;
}
}
}
//
// readALS31300ADC_FastLoop
//
// Read the X, Y, Z 8 bit values from Register 0x28
// eight times quickly using the fast loop mode.
//
void readALS31300ADC_FastLoop(int busAddress)
{
uint32_t value0x27;
// Read the register the I2C loop mode is in
uint16_t error = read(busAddress, 0x27, value0x27);
if (error != kNOERROR)
{
Serial.print("Unable to read the ALS31300. error = ");
Serial.println(error);
}
// I2C loop mode is in bits 2 and 3 so mask them out
// and set them to the fast loop mode
value0x27 = (value0x27 & 0xFFFFFFF3) | (0x1 << 2);
// Write the new values to the register the I2C loop mode is in
error = write(busAddress, 0x27, value0x27);
if (error != kNOERROR)
{
Serial.print("Unable to read the ALS31300. error = ");
Serial.println(error);
}
// Write the register address that is going to be read from the ALS31300 (0x28)
Wire.beginTransmission(busAddress);
Wire.write(0x28);
error = Wire.endTransmission(false);
// The ALS31300 accepted the address
if (error == kNOERROR)
{
int x;
int y;
int z;
// Eight times is arbitrary, there is no limit. What is being demonstrated
// is that once the address is set to 0x28, all reads will be from 0x28 until the
// register address is changed or the loop mode is changed.
xd = 0;
yd = 0;
zd = 0;
for (int count = 0; count < 8; ++count)
{
// Start the read and request 4 bytes
// which is the contents of register 0x28
Wire.requestFrom(busAddress, 4);
// Read the first 4 bytes which are the contents of register 0x28
// and sign extend the 8th bit
x = SignExtendBitfield(Wire.read(), 8);
y = SignExtendBitfield(Wire.read(), 8);
z = SignExtendBitfield(Wire.read(), 8);
Wire.read(); // Temperature and flags not used
xd += x;
yd += y;
zd += z;
// Display the values of x, y and z
/*
Serial.print("Count, X, Y, Z = ");
Serial.print(count);
Serial.print(", ");
Serial.print(x);
Serial.print(", ");
Serial.print(y);
Serial.print(", ");
Serial.println(z);
*/
// Convert the X, Y and Z values into radians
float rx = (float)x / 256.0 * M_TWOPI;
float ry = (float)y / 256.0 * M_TWOPI;
float rz = (float)z / 256.0 * M_TWOPI;
// Use a four quadrant Arc Tan to convert 2
// axis to an angle (which is in radians) then
// convert the angle from radians to degrees
// for display.
float angleXY = atan2f(ry, rx) * 180.0 / M_PI;
float angleXZ = atan2f(rz, rx) * 180.0 / M_PI;
float angleYZ = atan2f(rz, ry) * 180.0 / M_PI;
/*
*
Serial.print("angleXY, angleXZ, angleYZ = ");
Serial.print(angleXY);
Serial.print(", ");
Serial.print(angleXZ);
Serial.print(", ");
Serial.print(angleYZ);
Serial.println(" Degrees");
*/
}
}
else
{
Serial.print("Unable to read the ALS31300. error = ");
Serial.println(error);
}
}
//
// readALS31300ADC_FullLoop
//
// Read the X, Y, Z 12 bit values from Register 0x28 and 0x29
// eight times quickly using the full loop mode.
//
void readALS31300ADC_FullLoop(int busAddress)
{
uint32_t value0x27;
// Read the register the I2C loop mode is in
uint16_t error = read(busAddress, 0x27, value0x27);
if (error != kNOERROR)
{
Serial.print("Unable to read the ALS31300. error = ");
Serial.println(error);
}
// I2C loop mode is in bits 2 and 3 so mask them out
// and set them to the full loop mode
value0x27 = (value0x27 & 0xFFFFFFF3) | (0x2 << 2);
// Write the new values to the register the I2C loop mode is in
error = write(busAddress, 0x27, value0x27);
if (error != kNOERROR)
{
Serial.print("Unable to read the ALS31300. error = ");
Serial.println(error);
}
// Write the address that is going to be read from the ALS31300
Wire.beginTransmission(busAddress);
Wire.write(0x28);
error = Wire.endTransmission(false);
// The ALS31300 accepted the address
if (error == kNOERROR)
{
int x;
int y;
int z;
// Eight times is arbitrary, there is no limit. What is being demonstrated
// is that once the address is set to 0x28, the first four bytes read will be from 0x28
// and the next four will be from 0x29 after that it starts all over at 0x28
// until the register address is changed or the loop mode is changed.
xd = 0;
yd = 0;
zd = 0;
for (int count = 0; count < 8; ++count)
{
// Start the read and request 8 bytes
// which is the contents of register 0x28 and 0x29
Wire.requestFrom(busAddress, 8);
// Read the first 4 bytes which are the contents of register 0x28
x = Wire.read() << 4;
y = Wire.read() << 4;
z = Wire.read() << 4;
Wire.read(); // Temperature and flags not used
// Read the next 4 bytes which are the contents of register 0x29
Wire.read(); // Upper byte not used
x |= Wire.read() & 0x0F;
byte d = Wire.read();
y |= (d >> 4) & 0x0F;
z |= d & 0x0F;
Wire.read(); // Temperature not used
// Sign extend the 12th bit for x, y and z.
x = SignExtendBitfield((uint32_t)x, 12);
y = SignExtendBitfield((uint32_t)y, 12);
z = SignExtendBitfield((uint32_t)z, 12);
xd += x;
yd += y;
zd += z;
/*
// Display the values of x, y and z
Serial.print("Count, X, Y, Z = ");
Serial.print(count);
Serial.print(", ");
Serial.print(x);
Serial.print(", ");
Serial.print(y);
Serial.print(", ");
Serial.println(z);
*/
// Convert the X, Y and Z values into radians
float rx = (float)x / 4096.0 * M_TWOPI;
float ry = (float)y / 4096.0 * M_TWOPI;
float rz = (float)z / 4096.0 * M_TWOPI;
// Use a four quadrant Arc Tan to convert 2
// axis to an angle (which is in radians) then
// convert the angle from radians to degrees
// for display.
float angleXY = atan2f(ry, rx) * 180.0 / M_PI;
float angleXZ = atan2f(rz, rx) * 180.0 / M_PI;
float angleYZ = atan2f(rz, ry) * 180.0 / M_PI;
Serial.print("angleXY, angleXZ, angleYZ = ");
Serial.print(angleXY);
Serial.print(", ");
Serial.print(angleXZ);
Serial.print(", ");
Serial.print(angleYZ);
Serial.println(" Degrees");
}
}
else
{
Serial.print("Unable to read the ALS31300. error = ");
Serial.println(error);
}
}
//
// read
//
// Using I2C, read 32 bits of data from the address on the device at the bus address
//
uint16_t read(int busAddress, uint8_t address, uint32_t& value)
{
// Write the address that is to be read to the device
Wire.beginTransmission(busAddress);
Wire.write(address);
int error = Wire.endTransmission(false);
// if the device accepted the address,
// request 4 bytes from the device
// and then read them, MSB first
if (error == kNOERROR)
{
Wire.requestFrom(busAddress, 4);
value = Wire.read() << 24;
value += Wire.read() << 16;
value += Wire.read() << 8;
value += Wire.read();
}
return error;
}
//
// write
//
// Using I2C, write 32 bit data to an address to the device at the bus address
//
uint16_t write(int busAddress, uint8_t address, uint32_t value)
{
// Write the address that is to be written to the device
// and then the 4 bytes of data, MSB first
Wire.beginTransmission(busAddress);
Wire.write(address);
Wire.write((byte)(value >> 24));
Wire.write((byte)(value >> 16));
Wire.write((byte)(value >> 8));
Wire.write((byte)(value));
return Wire.endTransmission();
}
//
// SignExtendBitfield
//
// Sign extend a right justified value
//
long SignExtendBitfield(uint32_t data, int width)
{
long x = (long)data;
long mask = 1L << (width - 1);
if (width < 32)
{
x = x & ((1 << width) - 1); // make sure the upper bits are zero
}
return (long)((x ^ mask) - mask);
}