I have a custom PCBA that is built around an STM32F103CBT6 microcontroller, and I need to read temperatures from a DS18b20 sensor.
I am using the STM32Duino core, not the rogerclark one.
I have two libraries that govern this, the Arduino-Temperature-Control-Library by MilesBurton and OneWire by PaulS
My sensor is wired as such:
Pin 1: 5V+
Pin 2: Pullup to 5V with 4.7K resistor, and also goes to the microcontroller PA8 pin
Pin 3: Ground
When my code first runs, I read -196, indicating a bad connection possibly. However, if I unplug the sensor and plug it back in, I get a reading. The reading then works just fine, it goes up and down when I touch the sensor, and is accurate as far as I can tell.
So, it appears that the code is initializing the sensor alright, but reading it properly is requiring a power cycle of the sensor? It's just odd. I've tried to read on all of the other forums and the github for STM32Duino and tried various suggestions from those posts but haven't had any luck.
Could it be because I've wired the sensor to 5V, when I really should have wired it to 3.3V to be the same as the microcontroller? What do you guys think? Have any of you run one of these sensors off a STM32F103CBT6 successfully? I'm locked into this sensor because I'm updating a legacy product to use a new microcontroller, otherwise I'd just use a thermistor and call it a day.
Here's my code segments.
Declaration:
Code: Select all
#define SENSORPIN PA8
OneWire Bus1(SENSORPIN); // Create a 1-wire object
DallasTemperature sensor1(&Bus1);
Code: Select all
sensor1.begin();
delay(750);
sensor1.requestTemperatures();
Code: Select all
void gettemps()
{
sensor1.requestTemperatures(); // Send the command to get temperatures
temp1 = sensor1.getTempFByIndex(0);
//temp1 is an int declared globally
}