Getting data from sensor registers (Alphasense OPC-R2) working with Arduino Uno but not with ESP32?

I'm trying to get the histogram data from the OPC-R2 Sensor from Alphasense.
Unfortunatelly there is no library for the OPC-R2 to just use with the Arduino Framework.

So I've written my own code to get the data with SPI Commands that are documented in the datasheet. The histogram data is 64 bytes long and I'm storing it in an unsigned char array.

The datatypes vary from float values occupying 4 bytes to unsigned 8bit integers occupying 1 byte and also unsigned 16bit integers occupying 2 bytes.

Im using pointers to store the values and print them to the serial monitor.

uint16_t *pUInt16;
float *pFloat;

The single byte 8bit integers Im printing directly without storing them.

The code is working fine with an Arduino UNO but as soon as I use the same code with the ESP32 I get too high values!?

Example output with ESP32:

16523,1660,312,2,0,0,0,0,0,0,0,0,0,0,0,0,0,38.33,0.00,0.00,0.00,4.400,25.6,30.9,0.289,141,0,145.865,164.662,164.698,27971
Checksum OK

Example output with Arduino UNO:

25970,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,56.67,0.00,0.00,0.00,4.500,25.2,29.8,0.285,1,0,0.245,0.299,0.299,59154
Checksum OK

The Datalabels are:

Time(ms),Bin00,Bin01,Bin02,Bin03,Bin04,Bin05,Bin06,Bin07,Bin08,Bin09,Bin10,Bin11,Bin12,Bin13,Bin14,Bin15,MToF Bin1,MToF
Bin3,MToF Bin5,MToF Bin7,SFR,Temp(C),RH(%),Sampling Period(s),Reject count Glitch,Reject count Long,PM_A(ug/m3),PM_B(ug/
m3),PM_C(ug/m3),Checksum

Especially the penultimate three values (PM1, PM2.5, PM10 values) and the first few Bins are way too high on the ESP32.

Here is an example of how I store the values inside the pointers:

    port.print(F("M_B (Particle mass Concentration B),"));
    pFloat = (float *)&SPI_in[13];
    port.println(*pFloat, 2);

Does this have anything to do with the architecture of the processors? (UNO=8bit esp32=32bit)

My Code is on Github:

It might be. 32bit processors often cannot access a 16 or 32bit value on odd addresses. As your float starts at byte 13 this may pose problems. Copy the 4 values in a separate 4 byte array and convert them on that array. Does that help?

Pretty sure they can, but perhaps not as efficiently. And even in the case where the hardware is unable to, the compiler would generate the code necessary to make it work.

Why don't you create a struct consisting of the proper datatypes and write the bytes from SPI directly into that? Then you could simply read the various fields directly. Be sure to define the struct with the 'packed' attribute:

struct __attribute__((packed)) TheStruct {
1 Like

Thx for that suggestion I will try that.

By the way, I've printed the individual register values arriving in hexadecimal via SPI and it seems wrong values are already arriving via the SPI interface. Here is an example:
Arduino UNO (correct values):
Single bytes: 72 3A 9E 3F => corresponds to 1,236
ESP32 TTGO T7 (wrong values):
Single bytes: 31 52 D3 42 => corresponds to 105,661 (way too high value)

Also, until now I have used the TTGO T7 1.5 board where there were said problems.
This one: TTGO-T7-Demo/T7V1.5.jpg at master · LilyGO/TTGO-T7-Demo · GitHub

Yesterday I used another ESP32 board (see picture). And with it it works without problems. The data arrive correctly?
I have also already tried 2 of the TTGO boards to exclude that the one is broken.

Why is it working with the ESP32 Board from the picture below but not with the TTGO T7 Board although both are ESP32 boards?

Maybe it's because your sensor seems to be a 5V device and constantly over-voltage the board might damage it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.