I only get temp -127 deg C when I try to use the sensor with a RP2040 Nano connect. If I move the sensor to an UNO board - same IO - and download the same sketch, it works fine. IO pin 12 (that ijust to make sure I havn't destraoyed this IO pin.
The wiring is as simple as it can be. The sensors black cable goes to ground, red to +3.3V and yellow to IO12. There is a pullup resistor (4,7k) between +3.3 and IO12
This is the code I am trying to use, found as an example of how to use a 18B20 with Arduino. I tried to use OneWire.h that was in the original example code but it crashed the Nano when downloading so I tried OneWireNg.h instead. This works for downloading but does not read the temperature (results in -127 deg C). The code works fine with the Uno board.
#include <OneWireNg.h>
#include <DallasTemperature.h>
// Data wire is conntec to the Arduino digital pin 12
#define ONE_WIRE_BUS 12
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
void setup(void)
{
// Start serial communication for debugging purposes
Serial.begin(9600);
// Start up the library
sensors.begin();
}
void loop(void){
// Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();
Serial.print("Celsius temperature: ");
// Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.println(sensors.getTempCByIndex(0));
delay(1000);
}
-127 indicates no connection, and that may be all that is wrong, so look there first. It may be that a smaller pullup resistor, maybe 3.3k is more appropriate with a 3.3v Arduino, but I have not heard of it. The DS18B20 is good down to 3.0v.
Could you provide me with the code you are using for this. I found this code at File>Examples>OneWireNg>Arduino>DallasTemperature but only get (moved my sensor to pin 13)
TIMESTAMP -> -----------
/*
* Copyright (c) 2019-2022 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
* see accompanying file LICENSE for details.
*
* This software is distributed WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the License for more information.
*/
/**
* Dallas family thermometers access example (Arduino).
*
* Required configuration:
* - @c CONFIG_SEARCH_ENABLED for non single sensor setup,
* - @c CONFIG_PWR_CTRL_ENABLED if @c PWR_CTRL_PIN is configured.
*/
#include "OneWireNg_CurrentPlatform.h"
#include "drivers/DSTherm.h"
#include "utils/Placeholder.h"
/*
* 1-wire bus pin number.
*/
#ifndef OW_PIN
# define OW_PIN 13
#endif
/*
* If defined: sensors powered parasitically.
*/
//#define PARASITE_POWER
/*
* If defined: only one sensor device is allowed to be connected to the bus.
* The library may be configured with 1-wire search activity disabled to
* reduce its footprint.
*/
//#define SINGLE_SENSOR
/*
* For parasitically powered sensors the parameter specifies type of power
* provisioning:
* - Not defined: power provided by bus pin.
* - Defined: power provided by a switching transistor and controlled by the
* pin number specified by the parameter.
*/
//#define PWR_CTRL_PIN 9
/*
* If defined: set permanent, common resolution for all sensors on the bus.
* Resolution may vary from 9 to 12 bits.
*/
//#define COMMON_RES (DSTherm::RES_12_BIT)
#if !defined(SINGLE_SENSOR) && !CONFIG_SEARCH_ENABLED
# error "CONFIG_SEARCH_ENABLED is required for non single sensor setup"
#endif
#if defined(PWR_CTRL_PIN) && !CONFIG_PWR_CTRL_ENABLED
# error "CONFIG_PWR_CTRL_ENABLED is required if PWR_CTRL_PIN is configured"
#endif
#if (CONFIG_MAX_SEARCH_FILTERS > 0)
static_assert(CONFIG_MAX_SEARCH_FILTERS >= DSTherm::SUPPORTED_SLAVES_NUM,
"CONFIG_MAX_SEARCH_FILTERS too small");
#endif
#ifdef PARASITE_POWER
# define PARASITE_POWER_ARG true
#else
# define PARASITE_POWER_ARG false
#endif
static Placeholder<OneWireNg_CurrentPlatform> ow;
/* returns false if not supported */
static bool printId(const OneWireNg::Id& id)
{
const char *name = DSTherm::getFamilyName(id);
Serial.print(id[0], HEX);
for (size_t i = 1; i < sizeof(OneWireNg::Id); i++) {
Serial.print(':');
Serial.print(id[i], HEX);
}
if (name) {
Serial.print(" -> ");
Serial.print(name);
}
Serial.println();
return (name != NULL);
}
static void printScratchpad(const DSTherm::Scratchpad& scrpd)
{
const uint8_t *scrpd_raw = scrpd.getRaw();
Serial.print(" Scratchpad:");
for (size_t i = 0; i < DSTherm::Scratchpad::LENGTH; i++) {
Serial.print(!i ? ' ' : ':');
Serial.print(scrpd_raw[i], HEX);
}
Serial.print("; Th:");
Serial.print(scrpd.getTh());
Serial.print("; Tl:");
Serial.print(scrpd.getTl());
Serial.print("; Resolution:");
Serial.print(9 + (int)(scrpd.getResolution() - DSTherm::RES_9_BIT));
long temp = scrpd.getTemp();
Serial.print("; Temp:");
if (temp < 0) {
temp = -temp;
Serial.print('-');
}
Serial.print(temp / 1000);
Serial.print('.');
Serial.print(temp % 1000);
Serial.print(" C");
Serial.println();
}
void setup()
{
#ifdef PWR_CTRL_PIN
new (&ow) OneWireNg_CurrentPlatform(OW_PIN, PWR_CTRL_PIN, false);
#else
new (&ow) OneWireNg_CurrentPlatform(OW_PIN, false);
#endif
DSTherm drv(ow);
Serial.begin(115200);
#if (CONFIG_MAX_SEARCH_FILTERS > 0)
drv.filterSupportedSlaves();
#endif
#ifdef COMMON_RES
/*
* Set common resolution for all sensors.
* Th, Tl (high/low alarm triggers) are set to 0.
*/
drv.writeScratchpadAll(0, 0, COMMON_RES);
/*
* The configuration above is stored in volatile sensors scratchpad
* memory and will be lost after power unplug. Therefore store the
* configuration permanently in sensors EEPROM.
*/
drv.copyScratchpadAll(PARASITE_POWER_ARG);
#endif
}
void loop()
{
DSTherm drv(ow);
/* convert temperature on all sensors connected... */
drv.convertTempAll(DSTherm::MAX_CONV_TIME, PARASITE_POWER_ARG);
#ifdef SINGLE_SENSOR
/* single sensor environment */
/*
* Scratchpad placeholder is static to allow reuse of the associated
* sensor id while reissuing readScratchpadSingle() calls.
* Note, due to its storage class the placeholder is zero initialized.
*/
static Placeholder<DSTherm::Scratchpad> scrpd;
OneWireNg::ErrorCode ec = drv.readScratchpadSingle(scrpd);
if (ec == OneWireNg::EC_SUCCESS) {
printId(scrpd->getId());
printScratchpad(scrpd);
} else if (ec == OneWireNg::EC_CRC_ERROR)
Serial.println(" CRC error.");
#else
/* read sensors one-by-one */
Placeholder<DSTherm::Scratchpad> scrpd;
for (const auto& id: *ow) {
if (printId(id)) {
if (drv.readScratchpad(id, scrpd) == OneWireNg::EC_SUCCESS)
printScratchpad(scrpd);
else
Serial.println(" Read scratchpad error.");
}
}
#endif
Serial.println("----------");
delay(1000);
}
the program was File>Examples>OneWireNg>Arduino>DallasTemperature with the DS18B20 1-wire bus connected to pin 13 - no changes made to code
/*
* Copyright (c) 2019-2022 Piotr Stolarz
* OneWireNg: Arduino 1-wire service library
*
* Distributed under the 2-clause BSD License (the License)
* see accompanying file LICENSE for details.
*
* This software is distributed WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the License for more information.
*/
/**
* Dallas family thermometers access example (Arduino).
*
* Required configuration:
* - @c CONFIG_SEARCH_ENABLED for non single sensor setup,
* - @c CONFIG_PWR_CTRL_ENABLED if @c PWR_CTRL_PIN is configured.
*/
#include "OneWireNg_CurrentPlatform.h"
#include "drivers/DSTherm.h"
#include "utils/Placeholder.h"
/*
* 1-wire bus pin number.
*/
#ifndef OW_PIN
# define OW_PIN 13
#endif
/*
* If defined: sensors powered parasitically.
*/
//#define PARASITE_POWER
/*
* If defined: only one sensor device is allowed to be connected to the bus.
* The library may be configured with 1-wire search activity disabled to
* reduce its footprint.
*/
//#define SINGLE_SENSOR
/*
* For parasitically powered sensors the parameter specifies type of power
* provisioning:
* - Not defined: power provided by bus pin.
* - Defined: power provided by a switching transistor and controlled by the
* pin number specified by the parameter.
*/
//#define PWR_CTRL_PIN 9
/*
* If defined: set permanent, common resolution for all sensors on the bus.
* Resolution may vary from 9 to 12 bits.
*/
//#define COMMON_RES (DSTherm::RES_12_BIT)
#if !defined(SINGLE_SENSOR) && !CONFIG_SEARCH_ENABLED
# error "CONFIG_SEARCH_ENABLED is required for non single sensor setup"
#endif
#if defined(PWR_CTRL_PIN) && !CONFIG_PWR_CTRL_ENABLED
# error "CONFIG_PWR_CTRL_ENABLED is required if PWR_CTRL_PIN is configured"
#endif
#if (CONFIG_MAX_SEARCH_FILTERS > 0)
static_assert(CONFIG_MAX_SEARCH_FILTERS >= DSTherm::SUPPORTED_SLAVES_NUM,
"CONFIG_MAX_SEARCH_FILTERS too small");
#endif
#ifdef PARASITE_POWER
# define PARASITE_POWER_ARG true
#else
# define PARASITE_POWER_ARG false
#endif
static Placeholder<OneWireNg_CurrentPlatform> ow;
/* returns false if not supported */
static bool printId(const OneWireNg::Id& id)
{
const char *name = DSTherm::getFamilyName(id);
Serial.print(id[0], HEX);
for (size_t i = 1; i < sizeof(OneWireNg::Id); i++) {
Serial.print(':');
Serial.print(id[i], HEX);
}
if (name) {
Serial.print(" -> ");
Serial.print(name);
}
Serial.println();
return (name != NULL);
}
static void printScratchpad(const DSTherm::Scratchpad& scrpd)
{
const uint8_t *scrpd_raw = scrpd.getRaw();
Serial.print(" Scratchpad:");
for (size_t i = 0; i < DSTherm::Scratchpad::LENGTH; i++) {
Serial.print(!i ? ' ' : ':');
Serial.print(scrpd_raw[i], HEX);
}
Serial.print("; Th:");
Serial.print(scrpd.getTh());
Serial.print("; Tl:");
Serial.print(scrpd.getTl());
Serial.print("; Resolution:");
Serial.print(9 + (int)(scrpd.getResolution() - DSTherm::RES_9_BIT));
long temp = scrpd.getTemp();
Serial.print("; Temp:");
if (temp < 0) {
temp = -temp;
Serial.print('-');
}
Serial.print(temp / 1000);
Serial.print('.');
Serial.print(temp % 1000);
Serial.print(" C");
Serial.println();
}
void setup()
{
#ifdef PWR_CTRL_PIN
new (&ow) OneWireNg_CurrentPlatform(OW_PIN, PWR_CTRL_PIN, false);
#else
new (&ow) OneWireNg_CurrentPlatform(OW_PIN, false);
#endif
DSTherm drv(ow);
Serial.begin(115200);
#if (CONFIG_MAX_SEARCH_FILTERS > 0)
drv.filterSupportedSlaves();
#endif
#ifdef COMMON_RES
/*
* Set common resolution for all sensors.
* Th, Tl (high/low alarm triggers) are set to 0.
*/
drv.writeScratchpadAll(0, 0, COMMON_RES);
/*
* The configuration above is stored in volatile sensors scratchpad
* memory and will be lost after power unplug. Therefore store the
* configuration permanently in sensors EEPROM.
*/
drv.copyScratchpadAll(PARASITE_POWER_ARG);
#endif
}
void loop()
{
DSTherm drv(ow);
/* convert temperature on all sensors connected... */
drv.convertTempAll(DSTherm::MAX_CONV_TIME, PARASITE_POWER_ARG);
#ifdef SINGLE_SENSOR
/* single sensor environment */
/*
* Scratchpad placeholder is static to allow reuse of the associated
* sensor id while reissuing readScratchpadSingle() calls.
* Note, due to its storage class the placeholder is zero initialized.
*/
static Placeholder<DSTherm::Scratchpad> scrpd;
OneWireNg::ErrorCode ec = drv.readScratchpadSingle(scrpd);
if (ec == OneWireNg::EC_SUCCESS) {
printId(scrpd->getId());
printScratchpad(scrpd);
} else if (ec == OneWireNg::EC_CRC_ERROR)
Serial.println(" CRC error.");
#else
/* read sensors one-by-one */
Placeholder<DSTherm::Scratchpad> scrpd;
for (const auto& id: *ow) {
if (printId(id)) {
if (drv.readScratchpad(id, scrpd) == OneWireNg::EC_SUCCESS)
printScratchpad(scrpd);
else
Serial.println(" Read scratchpad error.");
}
}
#endif
Serial.println("----------");
delay(1000);
}
looking at your photo and the rp2040-01-technical-reference pinout it looks like you have connected the DS18B20 1-wire bus connected to pin D13 which is GPIO6
GPIO13 is connected to pin D19
try changing the pin definition in your code to use GPIO6
or move the DS18B20 1-wire bus to D19 to use GPIO13
avoid posting images of screen shots and serial output - images take a lot of space and cannot be copied from
post using code tags -select < CODE/ > and paste text where it says “type or paste code here”