-
• #2
Yes - just follow the examples here: http://www.espruino.com/I2C
-
• #3
OK , great
1 more question before I start with the code
where do I downlaod the require library for the device I need?
where do I search if one exist ?Thanks ,
-
• #4
Best bet is to use the search box in the top right of https://www.espruino.com/
Usually if there's an existing driver for something it'll be listed there
-
• #5
and if not
what can I do?I want to use the max30102 - but there is no "driver" for this
I have made a cool project with this device using Arduino now I want to use the Espruino
or maybe you can advise about a humen temperture sensor? -
• #6
Then you can write your own module for it, eg: http://www.espruino.com/Writing+Modules
All the source code for other modules is available from their respective pages so it's pretty easy to see what is done and work from there.
As it happens, I have some beta MAX30102 code that hasn't been added as a module yet:
var hrm = (function() { // MAX30102 var R = { INTR_STATUS_1 : 0x00, INTR_STATUS_2 : 0x01, INTR_ENABLE_1 : 0x02, INTR_ENABLE_2 : 0x03, FIFO_WR_PTR : 0x04, OVF_COUNTER : 0x05, FIFO_RD_PTR : 0x06, FIFO_DATA : 0x07, FIFO_CONFIG : 0x08, MODE_CONFIG : 0x09, SPO2_CONFIG : 0x0A, LED1_PA : 0x0C, LED2_PA : 0x0D, PILOT_PA : 0x10, MULTI_LED_CTRL1 : 0x11, MULTI_LED_CTRL2 : 0x12, TEMP_INTR : 0x1F, TEMP_FRAC : 0x20, TEMP_CONFIG : 0x21, PROX_INT_THRESH : 0x30, REV_ID : 0xFE, PART_ID : 0xFF }; var i = new I2C(); i.setup({scl:P.HRM_SCL,sda:P.HRM_SDA}); function r(a,n) { i.writeTo(87,a); return i.readFrom(87,n); } function w(a,d) { i.writeTo(87,[a,d]); } function init() { if (r(R.PART_ID,1)!=0x15) throw new Error("WHO_AM_I failed"); w(R.MODE_CONFIG,0x40); // reset all w(R.INTR_ENABLE_1,0xc0); // INTR setting w(R.INTR_ENABLE_2,0x00); w(R.FIFO_WR_PTR,0x00); //FIFO_WR_PTR[4:0] w(R.OVF_COUNTER,0x00); //OVF_COUNTER[4:0] w(R.FIFO_RD_PTR,0x00); //FIFO_RD_PTR[4:0] w(R.FIFO_CONFIG,0b10001111); //sample avg = 4, fifo rollover=false, fifo almost full = 17 w(R.MODE_CONFIG,0x02); //0x02 for Red only, 0x03 for SpO2 mode 0x07 multimode LED w(R.SPO2_CONFIG,0b00101111); // SPO2_ADC range = 4096nA, SPO2 sample rate (400 Hz), LED pulseWidth (411uS) w(R.LED1_PA,0x3f); //Choose value for ~ 7mA for LED1 w(R.LED2_PA,0x3f); // Choose value for ~ 7mA for LED2 w(R.PILOT_PA,0x3f); // Choose value for ~ 8mA for Pilot LED (?)( } function kill() { w(R.MODE_CONFIG,0x40); } function read() { var d = r(R.FIFO_DATA,6); return { red : (d[0]<<16)|(d[1]<<8)|d[2], ir : (d[3]<<16)|(d[4]<<8)|d[5] }; } function readFifoRaw() { var fifo = r(R.FIFO_WR_PTR,3); // wr,overflow,rd var s = fifo[0]-fifo[2]; if (s<=0) s+=32; if (fifo[1]) s=32; //overflowed var result = []; return r(R.FIFO_DATA,3*s); } function decodeRawData(d, red) { var l = d.length; for (var i=0,n=0;i<l;i+=3,n++) { red[n] = (d[i+0]<<16)|(d[i+1]<<8)|d[i+2]; } } var h = { r:r,w:w, init:init, kill:kill, read:read, // read one readFifoRaw:readFifoRaw, // read entire fifo as raw data (32 elements max, 6 bytes each) decodeRawData:decodeRawData // decode data read with readFifoRaw }; // IRQ seems not to work at the moment //pinMode(P.HRM_INT,"input_pullup"); //setWatch(function() { // h.emit('data',read()); //}, P.HRM_INT, {repeat:true,edge:-1}); // could use with hrm.on('data',x=>print("hrm",x)); return h; })(); function getHRM() { print("Starting HRM..."); hrm.init(); var cnt = 100; var rawdata = new Uint8Array(cnt*3); var dcnt = 0; print("Waiting for HRM... wait 1 second"); var i = setInterval(function() { hrm.readFifoRaw(); }); setTimeout(function() { print("Reading HRM... wait 10 seconds"); clearInterval(i); hrm.readFifoRaw(); var t = getTime(); var i = setInterval(function() { d = hrm.readFifoRaw(); rawdata.set(d, dcnt*3); dcnt += d.length/3; if (dcnt>=cnt) { drawHRM(rawdata); dcnt=0; } }, 1000); }, 1000); } function drawHRM(rawdata) { var cnt = rawdata.length/3; var dred = new Uint32Array(cnt); hrm.decodeRawData(rawdata, dred); var g = Graphics.createArrayBuffer(cnt,60,1); var red = E.sum(dred)/cnt; g.clear(); g.drawString("Red",2,2); g.moveTo(-100,0); for (var i=0;i<cnt;i++) g.lineTo(i,30+(dred[i]-red)/10); g.dump(); }
If you want to find out about temperature sensors that are supported out of the box with Espruino, just type 'temperature' into the search box: http://www.espruino.com/Search?kw=temperature
-
• #7
what are what are the chances that you have this ? :-)
if I wnat to use the code you wrote , what do I need to do?
I just want to be able to read the tempThank you so much!
-
• #8
meanwhile I will just try to see I can connect
// set up I2C var i2c = new I2C(); i2c.setup({ scl : B6, sda: B7 });
what is B6\B7 - does it mean D6\D7 ?
and if I'm using serial also , can I connect it to D11 and D12?Thanks ,
-
• #9
That example uses an STM32 based Espruino board. You'll need to use your own pins.
The nRF52832 based boards (yours is one) can have I2C on any pin. You just need to specify it.
See for example https://www.espruino.com/INA219 but instead of A4/A5 you can use D6/D7 or D11/D12 or any pin you want.
And yes you can connect your serial to D11 and D12
-
• #10
great ,
wanted to be sureand do you also know how can I use the code
Gordon
upload?
what do I need to in order to connect the device?
Thanks ,
-
• #11
No sorry. I don't have that sensor.
-
• #12
You need to add:
var P = { HRM_SCL : scl_pin, P.HRM_SDA : sda_pin }; getHRM();
At the end of your code and it should work.
what do I need to in order to connect the device?
You said you had it connected to Arduino and working? Just connect it as you did there.
-
• #13
this is what I did:
// set up I2C var i2c = new I2C(); i2c.setup({ scl : D25, sda: D26 }); var P = { HRM_SCL : D25, HRM_SDA : D26 }; var hrm = (function() { // MAX30102 var R = { INTR_STATUS_1 : 0x00, INTR_STATUS_2 : 0x01, INTR_ENABLE_1 : 0x02, INTR_ENABLE_2 : 0x03, FIFO_WR_PTR : 0x04, OVF_COUNTER : 0x05, FIFO_RD_PTR : 0x06, FIFO_DATA : 0x07, FIFO_CONFIG : 0x08, MODE_CONFIG : 0x09, SPO2_CONFIG : 0x0A, LED1_PA : 0x0C, LED2_PA : 0x0D, PILOT_PA : 0x10, MULTI_LED_CTRL1 : 0x11, MULTI_LED_CTRL2 : 0x12, TEMP_INTR : 0x1F, TEMP_FRAC : 0x20, TEMP_CONFIG : 0x21, PROX_INT_THRESH : 0x30, REV_ID : 0xFE, PART_ID : 0xFF }; var i = new I2C(); i.setup({scl:P.HRM_SCL,sda:P.HRM_SDA}); function r(a,n) { i.writeTo(87,a); return i.readFrom(87,n); } function w(a,d) { i.writeTo(87,[a,d]); } function init() { if (r(R.PART_ID,1)!=0x15) throw new Error("WHO_AM_I failed"); w(R.MODE_CONFIG,0x40); // reset all w(R.INTR_ENABLE_1,0xc0); // INTR setting w(R.INTR_ENABLE_2,0x00); w(R.FIFO_WR_PTR,0x00); //FIFO_WR_PTR[4:0] w(R.OVF_COUNTER,0x00); //OVF_COUNTER[4:0] w(R.FIFO_RD_PTR,0x00); //FIFO_RD_PTR[4:0] w(R.FIFO_CONFIG,0b10001111); //sample avg = 4, fifo rollover=false, fifo almost full = 17 w(R.MODE_CONFIG,0x02); //0x02 for Red only, 0x03 for SpO2 mode 0x07 multimode LED w(R.SPO2_CONFIG,0b00101111); // SPO2_ADC range = 4096nA, SPO2 sample rate (400 Hz), LED pulseWidth (411uS) w(R.LED1_PA,0x3f); //Choose value for ~ 7mA for LED1 w(R.LED2_PA,0x3f); // Choose value for ~ 7mA for LED2 w(R.PILOT_PA,0x3f); // Choose value for ~ 8mA for Pilot LED (?)( } function kill() { w(R.MODE_CONFIG,0x40); } function read() { var d = r(R.FIFO_DATA,6); return { red : (d[0]<<16)|(d[1]<<8)|d[2], ir : (d[3]<<16)|(d[4]<<8)|d[5] }; } function readFifoRaw() { var fifo = r(R.FIFO_WR_PTR,3); // wr,overflow,rd var s = fifo[0]-fifo[2]; if (s<=0) s+=32; if (fifo[1]) s=32; //overflowed var result = []; return r(R.FIFO_DATA,3*s); } function decodeRawData(d, red) { var l = d.length; for (var i=0,n=0;i<l;i+=3,n++) { red[n] = (d[i+0]<<16)|(d[i+1]<<8)|d[i+2]; } } var h = { r:r,w:w, init:init, kill:kill, read:read, // read one readFifoRaw:readFifoRaw, // read entire fifo as raw data (32 elements max, 6 bytes each) decodeRawData:decodeRawData // decode data read with readFifoRaw }; // IRQ seems not to work at the moment //pinMode(P.HRM_INT,"input_pullup"); //setWatch(function() { // h.emit('data',read()); //}, P.HRM_INT, {repeat:true,edge:-1}); // could use with hrm.on('data',x=>print("hrm",x)); return h; })(); function getHRM() { print("Starting HRM..."); hrm.init(); var cnt = 100; var rawdata = new Uint8Array(cnt*3); var dcnt = 0; print("Waiting for HRM... wait 1 second"); var i = setInterval(function() { hrm.readFifoRaw(); }); setTimeout(function() { print("Reading HRM... wait 10 seconds"); clearInterval(i); hrm.readFifoRaw(); var t = getTime(); var i = setInterval(function() { d = hrm.readFifoRaw(); rawdata.set(d, dcnt*3); dcnt += d.length/3; if (dcnt>=cnt) { drawHRM(rawdata); dcnt=0; } }, 1000); }, 1000); } function drawHRM(rawdata) { var cnt = rawdata.length/3; var dred = new Uint32Array(cnt); hrm.decodeRawData(rawdata, dred); var g = Graphics.createArrayBuffer(cnt,60,1); var red = E.sum(dred)/cnt; g.clear(); g.drawString("Red",2,2); g.moveTo(-100,0); for (var i=0;i<cnt;i++) g.lineTo(i,30+(dred[i]-red)/10); g.dump(); } getHRM();
in the device (MAx30102):
connected 3.3V,GND
SCL-D25
SDA-D26after I uplaod the code I get this error:
>Starting HRM... Uncaught Error: WHO_AM_I failed at line 45 col 60 ...new Error("WHO_AM_I failed"); ^ in function "init" called from line 103 col 12 hrm.init(); ^ in function "getHRM" called from line 143 col 8 getHRM();
why?
what did I do wrong ?I disconnected the max3012 and connect it to the arduino - it's working (so now hardware issue ) . did it just to be sure
-
• #14
Hi - don't add:
var i2c = new I2C(); i2c.setup({ scl : D25, sda: D26 });
However I doubt that'd have had an effect.
Because they are already in the code lower down. "WHO_AM_I failed" means that it couldn't communicate with the device via I2C (it asked the device what it was, and it didn't return with the code 0x15).
You could try just:
var P = { HRM_SCL : D25, HRM_SDA : D26 }; var i = new I2C(); i.setup({scl:P.HRM_SCL,sda:P.HRM_SDA}); function r(a,n) { i.writeTo(87,a); return i.readFrom(87,n); } print("WHO_AM_I", r(0xFF,1));
And see if you can get it to print something that's not 255 by checking your wiring. That'd be the best way to start.
-
• #15
I get 255
>WHO_AM_I new Uint8Array([255])
everything is connected
D25,D26,GND,3.3V (to the same power as the MDBT) -
• #16
Well, something is wrong I'm afraid. You generally get 255 when there's no device connected at all.
It's possible you're using a breakout board that is designed for 5v operation, and Espruino only works at 3.3?
-
• #17
sometime you need to throw everythin and start from 0
took a new MAx30102 , new cables
now it's seem to be working
I get a grafh
So thank you for this!I don't need the grafh - what do to in order to just see the values of the temp?
is it the rawdata?
Thanks again! -
• #18
Honestly I think it's up to you to figure that out - now there's something that works it's not rocket science to look at the data that's there.
There is no temperature value though because the sensor is a heart rate sensor - it measures IR&red light and it can't work out temperature from that.
-
• #19
I know that the MAx30102 is also a temperture sensor
from the datasheet
https://datasheets.maximintegrated.com/en/ds/MAX30102.pdfINTERNAL DIE TEMPERATURE SENSOR Temperature ADC Acquisition Time TT TA
= +25°C 29 ms Temperature Sensor Accuracy TA TA = +25°C ±1 °C Temperature Sensor Minimum Range TMIN -40 °C Temperature Sensor
Maximum Range TMAX 85 °CI will figure the data out - but now I want to be sure the what you wrote can "read" the temp data
can I connect to it an i2c device?
Thanks ,