Arduino, ESP and 1wire thermometers

Da raspibo.
Versione del 27 giu 2016 alle 11:08 di Renzo (discussione | contributi) (→‎Testing)
(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)
Jump to navigation Jump to search

This is a simple sketch to read the current temperature from a DS18B10/DS18B20 1 wire thermometer.

I have successfully tested this program on an ESP-12 (ESP8266) using the Arduino IDE.

hardware wiring

This project uses a "console cable" (but any USB to TTL converter is okay). Using an Adafruit like cable:

  • Black -> GROUND
  • While -> TXD on ESP
  • Green -> RXD on ESP

DO NOT CONNECT THE RED PIN TO YOUR ESP! The red pin is +5V while ESP need +3V3 so either you have a step-down circuit to convert 5V to 3.3V or you'll need another power source of the right voltage for yout ESP.

The connection to the DS18B10 is simple:

TO-92 Front with Pin Numbers.png

  • 1 is GND -> GND
  • 2 is DATA -> GP02 on ESP
  • 3 is 3v3 -> +3V3

The circuit also needs a 4.7K resistor between DATA e 3v3.

Software

Here is the source code.

#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2  // DS18B20 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);

void setup(void){
  Serial.begin(115200);
  pinMode(led, OUTPUT);
  digitalWrite(led, 0);
}

void loop(void){
  float temp;
  DS18B20.requestTemperatures();
  temp = DS18B20.getTempCByIndex(0);
  Serial.print("Temperature: ");
  Serial.println(temp);

  delay(1000);
}

Testing

Load the program on you ESP. Remember that GP00 must be connected to ground at power-up to set the ESP in "flashing mode".

Open a terminal (e.g. using screen)

screen /dev/ttyUSB0 115200

... and you'll get the temperature (one reading per second).

Unplug GP00 otherwise at the next power cycle your ESP will be in "flashing mode" again. If GP00 is unconnected, the program is restarted at power up.

This is the output (one line per second):

Temperature: 31.94
Temperature: 31.94
Temperature: 32.00
Temperature: 32.00
Temperature: 32.00
Temperature: 32.00
Temperature: 32.00
Temperature: 32.06
Temperature: 32.25
Temperature: 32.44
Temperature: 32.56
Temperature: 32.69
Temperature: 32.63
Temperature: 32.50
Temperature: 32.44

(I touched the thermometer, so the temperature raised).