ckck
February 3, 2017, 6:09pm
1
post-suffering update:
Thanks to everyone who read this! I'm sorry to say that I wasted your time. The reason was a faulty soldering on the 5V pin, so the sensor never booted. No wonder it worked with other Arduinos that I connected via breadboards.
Lesson learned: Don't try to troubleshoot after a 60h workweek. Go have a beer and see things with a fresh pair of eyes and a bit of hangover that prevents you from doing any fast decisions to begin with.
Hei all,
Forum-beginner here, so please don't grill me too hard
Here's the thing:
I have a micro OLED communicating via SPI and a Sensirion STS 30 temperature sensor communicating via i2c. It all works fine when I have them connected to an Arduino Uno, but when I connect it to an Arduino Micro, the temperature sensor is not working.
SDA / SCL is connected to D2 / D3 on the micro.
I'm using 10kOhm Pull-Up resistors for the sensor.
Is there any basic I2C difference between the two chips, apart from the pins? Do I need to set a pinmode different? Anything..?
If you have any idea what to look for, please let me know.. any advice is very helpful at this stage!
thanks!
pylon
February 3, 2017, 6:26pm
2
Post your sketch! (and use code tags, the "</>" button in the editor)
ckck
February 3, 2017, 6:35pm
3
#include <SFE_MicroOLED.h>
#include <math.h>
#include <PID_v1.h>
#include <SPI.h>
#include <Wire.h> // Used for I2C
#include <Adafruit_SHT31.h>
//-----------------------------
//STH30 Thermometer Definitions
Adafruit_SHT31 sts30 = Adafruit_SHT31();
int addrpin = 7;
double Tout = 0;
//STH30 Thermometer Definitions
//-----------------------------
//-----------------------------
//OLED Defintions
#define PIN_RESET 9 // Connect RST to pin 9
#define PIN_DC 8 // Connect DC to pin 8
#define PIN_CS 10 // Connect CS to pin 10
//D1 / MOSI ----------------- D11 (don't change, for Uno etc. only, for Arduino Micro use MOSI Pin)
//D0/SCK ------------------ D13 (don't change for Uno etc. only, for Arduino Micro use SCK Pin)
MicroOLED oled(PIN_RESET, PIN_DC, PIN_CS); // SPI declaration
int i = 0; //use for blink
//OLED Definitions
//-----------------------------
//-----------------------------
//Controller Defintions
#define cPin 5
int power = 0; //power level variable, 0-255
double dpower = 0;
//Define Variables we'll be connecting to
double targetT = 37.5;
//Specify the links and initial tuning parameters
//original values: double Kp = 15, Ki = 0.3, Kd = 3;
double Kp = 15, Ki = 0.3, Kd = 4;
PID myPID(&Tout, &dpower, &targetT, Kp, Ki, Kd, DIRECT);
//Controller Definitions
//-----------------------------
//-----------------------------
//other variables
int flowrate = 5;
//other variables
//-----------------------------
void setup() {
Serial.begin(9600);
//while (!Serial);
pinMode(A0, INPUT_PULLUP); //initialize buttons
//-----------------------------
//Controller Setup
pinMode(cPin, OUTPUT);
//turn the PID on
myPID.SetMode(AUTOMATIC);
//Controller Setup
//-----------------------------
//-----------------------------
//STH30 Thermometer Setup
pinMode(addrpin, OUTPUT);
digitalWrite(addrpin, LOW);
Serial.println("STS30 test");
if (!sts30.begin(0x4A)) { // Set to 0x45 for alternate i2c addr
Serial.println("Couldn't find STS30");
while (1) delay(1);
}
//STH30 Thermometer Setup
//-----------------------------
//-----------------------------
//OLED Setup
oled.begin(); // Initialize the OLED
oled.clear(ALL); // Clear the display's internal memory
oled.clear(PAGE); // Clear the buffer.
oled.setCursor(7, 15);
oled.setFontType(1);
oled.print("something");
oled.display();
delay(1500);
//OLED Setup
//-----------------------------
//-----------------------------
//Get Flow Rate
get_flowrate();
//Get Flow Rate
//-----------------------------
//-----------------------------
//Set Ki
Ki = setKi(flowrate);
myPID.SetTunings(Kp, Ki, Kd);
//Set Ki
//-----------------------------
//-----------------------------
//Get Target Temperature
get_temp();
//Get Target Temperature
//-----------------------------
//-----------------------------
//Set Running Screen
runningScreen();
//Set Running Screen
//-----------------------------
}
void loop() {
//-----------------------------
//OLED loop
if (i == 17) {
oled.rectFill(23, 31, 8, 16, BLACK, NORM);
oled.display();
}
if (i == 20) {
oled.setCursor(-1, 31);
oled.setFontType(2);
if (Tout <= 9)oled.print("0");
oled.print(Tout);
oled.setFontType(0);
oled.print("C");
oled.display();
i = 0;
}
i = i + 1;
//OLED loop
//-----------------------------
//-----------------------------
//STS30 loop
Tout = sts30.readTemperature();
if (! isnan(Tout)) { // check if 'is not a number'
Serial.print("Temp *C = "); Serial.println(Tout); //Serial.print("\t Time: "); Serial.println(ti);
} else {
Serial.println("Failed to read temperature");
}
//STS30 loop
//-----------------------------
//-----------------------------
//PID Loop
myPID.Compute();
power = dpower;
analogWrite(cPin, power);
//PID Loop
//-----------------------------
}
Here you go. the get_temp / get_flowrate are calling other functions that get a user input through some buttons. that's all working fine.
I'm using the Adafruit sht31 library, since the temperature sensor is the same but without the humidity sensor.
I might have changed the default address in the library (correct though, taken from the sensirion datasheet sts30, 0x4A).
thanks
ckck
February 4, 2017, 12:25pm
4
.. the problem was a bad 5V solder so the sensor never booted. Everything is running fine now, which gives me a very deep sensation of joy.
@moderators
I guess the topic could be deleted, however it might be beneficial for someone who is trying to hook up an STS30 sensor (like I did), since there are no libraries for that sensor. I did a lot of trial and error until I figured out that the SHT31 uses the same logic -> the same library can be used.
Here's the changes I did to it:
Old Adafruit_SHT31.cpp:
boolean Adafruit_SHT31::readTempHum(void) {
uint8_t readbuffer[6];
writeCommand(SHT31_MEAS_HIGHREP);
delay(500);
Wire.requestFrom(_i2caddr, (uint8_t)6);
if (Wire.available() != 3)
return false;
for (uint8_t i=0; i<6; i++) {
readbuffer[i] = Wire.read();
// Serial.print("0x"); Serial.println(readbuffer[i], HEX);
}
uint16_t ST, SRH;
ST = readbuffer[0];
ST <<= 8;
ST |= readbuffer[1];
if (readbuffer[2] != crc8(readbuffer, 2)) return false;
SRH = readbuffer[3];
SRH <<= 8;
SRH |= readbuffer[4];
if (readbuffer[5] != crc8(readbuffer+3, 2)) return false;
Serial.print("ST = "); Serial.println(ST);
double stemp = ST;
stemp *= 175;
stemp /= 0xffff;
stemp = -45 + stemp;
temp = stemp;
Serial.print("SRH = "); Serial.println(SRH);
double shum = SRH;
shum *= 100;
shum /= 0xFFFF;
humidity = shum;
return true;
}
New:
boolean Adafruit_SHT31::readTempHum(void) {
uint8_t readbuffer[3];
writeCommand(SHT31_MEAS_HIGHREP);
delay(50);
Wire.requestFrom(_i2caddr, (uint8_t)3);
if (Wire.available() != 3)
return false;
for (uint8_t i=0; i<3; i++) {
readbuffer[i] = Wire.read();
// Serial.print("0x"); Serial.println(readbuffer[i], HEX);
}
uint16_t ST, SRH;
ST = readbuffer[0];
ST <<= 8;
ST |= readbuffer[1];
if (readbuffer[2] != crc8(readbuffer, 2)) return false;
double stemp = ST;
stemp *= 175;
stemp /= 0xffff;
stemp = -45 + stemp;
temp = stemp;
return true;
}