Data logging and I/O control with Python over serial I2C

Hello,

I have the USB to I2C converter connected to a PC and three I2C device: temperature, 0-10V DAC], and 4 channel digital input. I would like to programmatically log and control these devices, hopefully using python. What would be the best method?

I have looked into pyserial, and was able to scan and find the serial port, but I’m not sure what to send/look for when communicating with the I2C devices on the other end. Any suggestions would be helpful. If this fails, I can use an Arduino as a middleman manager, and read it’s outgoing serial as shown here, but would like to skip this interim step.

I think that using visual studio is the supported option, but I am having difficulty finding manuals for the correct commands to use for reading/writing data.

Thanks!

-Teal

Hi Teal,
If you have USB to I2C converter then you can send serial commands.
https://ncd.io/serial-to-i2c-conversion/

The DAC is real simple devices.
Arduino sample code can be found here

Thanks

Hi Bhaskar,

Thank you very much for the code and the link to the git repos. I was able to successfully run the code, and receive the 0-10V output as I desired.

I did run into an issue when trying to run the SHT25 Temp sensor and the
4-channel input sensor example code. For the temp sensor, the error was " ‘sht’ was not declared in this scope" and for the 4-channel input sensor the error was " ‘pca’ was not declared in this scope". In both cases I downloaded the .h and .cpp files, and put them in the library directory as per these instructions.

Thanks,

Teal

Hi Teal,
I this arduino ide cant fine the .cpp and .h file.

You can put them in
C:\Program Files (x86)\Arduino\libraries

Thanks

Bhaskar,

I have placed the files in C:\Program Files (x86)\arduino-1.8.12\libraries\SHT25 and within that directory I have the .h and .cpp files. If I move the files to C:\Program Files (x86)\arduino-1.8.12\libraries then I receive the error “SHT25.h: No such file or directory”. Maybe I am missing a step or my code is incorrect? The exact code I am using is shown below:

#include <Wire.h>
#include <SHT25.h>

SHT25 sht;

void setup(void) 
{
    Serial.begin(9600);

    // The address can be changed making the option of connecting multiple devices
    sht.getAddr_SHT25(SHT25_DEFAULT_ADDRESS);   // 0x40

    // The Measurement Resolution, VDD Status, On-Chip Heater Status and Heater Current,
    // The Temperature and Humidity Measurement Modes can be changed via the following functions

    sht.setResolution(RESOLUTION_0);             // RH: 12 bit, Temp: 14 bit
    // sht.setResolution(RESOLUTION_1);          // RH: 8 bit, Temp: 12 bit
    // sht.setResolution(RESOLUTION_2);          // RH: 10 bit, Temp: 13 bit
    // sht.setResolution(RESOLUTION_4);          // RH: 11 bit, Temp: 11 bit

    sht.setVoltage(VOLTAGE_OK);                  // VDD OK
    // sht.setVoltage(VOLTAGE_LOW);              // VDD Low

    sht.setHeaterStatus(HEATER_ENABLE);          // On-chip Heater Enable
    // sht.setHeaterStatus(HEATER_DISABLE);      // On-chip Heater Disable

    sht.setOTPStatus(OTP_DISABLE);               // OTP Reload Disable
    // sht.setOTPStatus(OTP_ENABLE);             // OTP Reload Enable

    sht.setTempMode(TEMP_NO_HOLD);               // Measure Temperature, No Hold Master Mode
    // sht.setTempMode(TEMP_HOLD);               // Measure Temperature, Hold Master Mode

    sht.setHumidityMode(HUMIDITY_NO_HOLD);       // Measure Humidity, No Hold Master Mode
    // sht.setHumidityMode(HUMIDITY_HOLD);       // Measure Humidity, Hold Master Mode

    sht.begin();
    delay(500);
}

void loop(void)
{
    byte error;
    int8_t address;

    address = sht.sht_i2cAddress;
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
    if (error == 0)
    {
        float cTemp, fTemp, humidity;

        Serial.println("Getting Readings from SHT25");
        Serial.println(" ");
        // Read and print out the temperature, then convert to C and F scales
        cTemp = sht.Measure_Temperature();
        fTemp = cTemp * 1.8 + 32;

        // Read and print out the Relative Humidity, convert it to %RH
        humidity = sht.Measure_Humidity();

        // Output data to screen
        Serial.print("Temperature Reading in Celsius: ");
        Serial.print(cTemp);
        Serial.println(" °C");
        Serial.print("Temperature Reading in Fahrenheit: ");
        Serial.print(fTemp);
        Serial.println(" °F");
        Serial.print("Relative Humidity: ");
        Serial.print(humidity);
        Serial.println(" %RH");
        Serial.println(" ");
        Serial.println("        ***************************        ");
        Serial.println(" ");
    }
    else
    {
        Serial.println("SHT25 Disconnected!");
        Serial.println(" ");
        Serial.println("        ************        ");
        Serial.println(" ");
    }

    delay(1000);
}

Thanks!

did you restart the arduino ide ?

Bhaskar,

This time I did perform a restart. No difference in errors though. When the .h and .cpp files are within \libraries, Arduino can’t seem to find the files. When I create the SHT25 named folder inside \libraries, then I get the out of scope error.

Bhaskar,

I connected with NCD over email, and figured out a solution. For the SHT25 temp sensor, I used this code, and for the 4-channel input used this code (which worked after remembering to have common ground between the I2C bus power and the digital input power). Combined with the code you pointed me towards for the 0-10V DAC, all of my devices are working well.

Thanks,

Teal

1 Like