Working code snippets
1. read DS18B20 at interval. output via terminal. non-blocking code
#include <OneWire.h>
#include <DallasTemperature.h>
#include <elapsedMillis.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);
uint8_t SensorAdresse[8] = {0x28, 0x56, 0x2D, 0x11, 0x05, 0x00, 0x00, 0x14};
elapsedMillis SensorIntervall;
unsigned int Intervall = 1000;
void setup(void)
{
Serial.begin(9600);
sensor.begin();
sensor.setResolution(SensorAdresse, 10);
Serial.println("Ausgabeformat CSV, Trenner = ;");
Serial.println("Zeit [s]; Temperatur [°C]");
Serial.println("");
}
void loop(void)
{
sensor.setWaitForConversion(false);
sensor.requestTemperatures();
float tempC = sensor.getTempC(SensorAdresse);
sensor.setWaitForConversion(true);
if (SensorIntervall > Intervall)
{
Serial.print(millis()/1000);
Serial.print(";");
Serial.println(tempC);
SensorIntervall = 0;
}
}
2. Show DS3231/DS1307 time on 32x128 I2C OLED display
#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#include <RtcDS3231.h>
RtcDS3231<TwoWire> Rtc(Wire);
#define I2C_ADDRESS 0x3C
#define RST_PIN -1
SSD1306AsciiWire oled;
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printTimeOnly(const RtcDateTime& dt)
{
char datestring[11];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u:%02u:%02u"),
dt.Hour(),
dt.Minute(),
dt.Second() );
oled.println(datestring);
}
void printDateOnly(const RtcDateTime& dt)
{
char datestring[11];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u.%02u.%02u"),
dt.Day(),
dt.Month(),
dt.Year() - 2000);
oled.println(datestring);
}
void setup() {
Wire.begin();
Wire.setClock(400000L);
#if RST_PIN >= 0
oled.begin(&Adafruit128x32, I2C_ADDRESS, RST_PIN);
#else
oled.begin(&Adafruit128x32, I2C_ADDRESS);
#endif
oled.setFont(lcdnums12x16);
Rtc.Begin();
oled.clear();
}
void loop() {
RtcDateTime now = Rtc.GetDateTime();
oled.setCursor(0, 0);
oled.set1X();
printDateOnly(now);
oled.setCursor(0, 2);
oled.set1X();
printTimeOnly(now);
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.