0

Reading Data from STEMMA Soil Sensor
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Reading Data from STEMMA Soil Sensor

by bjornhnelson on Wed May 29, 2019 8:22 pm

Hello,

I am working on a soil moisture project using the Adafruit STEMMA I2C soil sensor (product id: 4026) and the MSP432 microcontroller. I am confused about how to read the moisture and temperature data values, we are using C to program our embedded system but the data sheet only explains how to integrate the STEMMA sensor with Python or Arduino using specific Adafruit libraries. Do I need to read certain memory registers from the STEMMA sensor to save the temperature and moisture data, and what are the addresses of these registers? A value of 255 is currently returned for most addresses when running my I2C read function, but I believe this may just be random data. I would appreciate any tips or troubleshooting advice on how to get the moisture sensor to work with my C code.

Thank you!

bjornhnelson
 
Posts: 4
Joined: Mon May 20, 2019 11:58 pm

Re: Reading Data from STEMMA Soil Sensor

by adafruit_support_mike on Fri May 31, 2019 3:07 am

The STEMMA has a full-scale microcontroller, so any register numbers you send it are virtual. The code on the microcontroller reads them and figures out what to send back.

Taking the example code from the library as a reference:

https://github.com/adafruit/Adafruit_Se ... xample.ino

The two functions that talk to the STEMMA are .getTemp() and .touchRead(). Those are both defined in the library's main .cpp file:

https://github.com/adafruit/Adafruit_Se ... #L651-L662
https://github.com/adafruit/Adafruit_Se ... #L260-L276

and the macros they use are defined in the header file:

https://github.com/adafruit/Adafruit_Se ... t_seesaw.h

lines 46, 86, 57, and 138.

In general, the Seesaw library uses two bytes per command: one to identify the general kind of information you want, and one to identfy the specific function in that group. The tutorial for the standalone Seesaw board has more information about the protocol:

https://learn.adafruit.com/adafruit-see ... w-platform

adafruit_support_mike
 
Posts: 58480
Joined: Thu Feb 11, 2010 2:51 pm

Re: Reading Data from STEMMA Soil Sensor

by bjornhnelson on Fri May 31, 2019 4:14 pm

From reading these files, it seems like I need to use a touch sensor base address of 0x0F and offset of 0x10 to read from the STEMMA using I2C. However, this read address is not working. What read address do I need to use? Can the STEMMA sensor be successfully interfaced with a microcontroller other than the Arduino? Right now, we have the STEMMA sensor connected directly to the MSP432 board (using 3.3V, GND, SCL, and SDA pins), and we are not using the Arduino seesaw board.

bjornhnelson
 
Posts: 4
Joined: Mon May 20, 2019 11:58 pm

Re: Reading Data from STEMMA Soil Sensor

by adafruit_support_mike on Sat Jun 01, 2019 3:49 am

The STEMMA sensor is an I2C device, so yes, it should work with any kind of microcontroller.

Post a photo showing your hardware and connections and we'll take a look. 800x600 images usually work best.

adafruit_support_mike
 
Posts: 58480
Joined: Thu Feb 11, 2010 2:51 pm

Re: Reading Data from STEMMA Soil Sensor

by bjornhnelson on Sat Jun 01, 2019 4:30 pm

Here is an attached photo of the pin connections. The Stemma sensor is connected to 3.3 V, ground, and the SCL (pin 1.7) and SDA (pin 1.6) pins on the MSP432 microcontroller. I have also included the I2C C code that we are using below. The soil sensor device address of 0x36 is passed into the initI2C() function call from main(), and a memory address of 0x0F10 was passed into the readI2C function call from main(). The readI2C function should be returning an accurate soil moisture value.

project.jpg
project.jpg (555.77 KiB) Viewed 161 times


Code: Select all | TOGGLE FULL SIZE
#include "msp.h"
static uint16_t TransmitFlag = 0;

// Initialize I2C bus for communicating with EEPROM.
void initI2C(uint8_t DeviceAddress)
{
  P1->SEL0 |= BIT6 | BIT7;                // Set I2C pins of eUSCI_B0

  // Enable eUSCIB0 interrupt in NVIC module
  NVIC->ISER[0] = 1 << ((EUSCIB0_IRQn) & 31);

  // Configure USCI_B0 for I2C mode
  EUSCI_B0->CTLW0 |= EUSCI_A_CTLW0_SWRST;   // Software reset enabled
  EUSCI_B0->CTLW0 = EUSCI_A_CTLW0_SWRST |   // Remain eUSCI in reset mode
          EUSCI_B_CTLW0_MODE_3 |            // I2C mode
          EUSCI_B_CTLW0_MST |               // Master mode
          EUSCI_B_CTLW0_SYNC |              // Sync mode
          EUSCI_B_CTLW0_SSEL__SMCLK;        // SMCLK

  EUSCI_B0->BRW = 30;                       // baudrate = SMCLK / 30 = 100kHz
  EUSCI_B0->I2CSA = DeviceAddress;          // Slave address
  EUSCI_B0->CTLW0 &= ~EUSCI_A_CTLW0_SWRST;  // Release eUSCI from reset

  EUSCI_B0->IE |= EUSCI_A_IE_RXIE |         // Enable receive interrupt
                  EUSCI_A_IE_TXIE;
}

uint8_t readI2C(uint16_t MemAddress)
{
  uint8_t ReceiveByte;
  uint8_t HiAddress;
  uint8_t LoAddress;

  HiAddress = MemAddress >> 8;
  LoAddress = MemAddress & 0xFF;

  EUSCI_B0->CTLW0 |= EUSCI_B_CTLW0_TR;      // Set transmit mode (write)
  EUSCI_B0->CTLW0 |= EUSCI_B_CTLW0_TXSTT;   // I2C start condition

  while (!TransmitFlag);                // Wait for EEPROM address to transmit
  TransmitFlag = 0;

  EUSCI_B0 -> TXBUF = HiAddress;    // Send the high byte of the memory address

  while (!TransmitFlag);            // Wait for the transmit to complete
  TransmitFlag = 0;

  EUSCI_B0 -> TXBUF = LoAddress;    // Send the low byte of the memory address

  while (!TransmitFlag);            // Wait for the transmit to complete
  TransmitFlag = 0;

  EUSCI_B0->CTLW0 &= ~EUSCI_B_CTLW0_TR;   // Set receive mode (read)
  EUSCI_B0->CTLW0 |= EUSCI_B_CTLW0_TXSTT; // I2C start condition (restart)

  // Wait for start to be transmitted
  while ((EUSCI_B0->CTLW0 & EUSCI_B_CTLW0_TXSTT));

  // set stop bit to trigger after first byte
  EUSCI_B0->CTLW0 |= EUSCI_B_CTLW0_TXSTP;

  while (!TransmitFlag);            // Wait to receive a byte
  TransmitFlag = 0;

  ReceiveByte = EUSCI_B0->RXBUF;    // Read byte from the buffer

  return ReceiveByte;
}

bjornhnelson
 
Posts: 4
Joined: Mon May 20, 2019 11:58 pm

Re: Reading Data from STEMMA Soil Sensor

by adafruit_support_mike on Mon Jun 03, 2019 12:05 am

I don't know the MSP432 architecture, so I can't tell you anything about the general validity of that code.

The one thing that does stand out is readI2C()'s return type of uint8_t.

If you check our code, the return type of touchRead() is uint16_t. The function reads two bytes from the STEMMA module and builds a 16-bit integer from them.

adafruit_support_mike
 
Posts: 58480
Joined: Thu Feb 11, 2010 2:51 pm

Re: Reading Data from STEMMA Soil Sensor

by bjornhnelson on Mon Jun 03, 2019 3:30 pm

Okay. To clarify, is 0x0F10 the correct address for reading a moisture value?

bjornhnelson
 
Posts: 4
Joined: Mon May 20, 2019 11:58 pm

Re: Reading Data from STEMMA Soil Sensor

by adafruit_support_mike on Tue Jun 04, 2019 2:25 am

Yes. 0x0F is the ID for the capacitive touch feature, and 0x10 is the ID for touch input 0, which the STEMMA uses.

adafruit_support_mike
 
Posts: 58480
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.