Bonjour à tous, (et au passage bonne année!)
Je suis en train de concevoir un montage consistant à afficher la température (et l'heure) sur un petit écran OLED en passant par un arduino pro mini.
Mais l'afficheur OLED affiche " 0°C 0% ", l'arduino pro mini n'arrive pas à lire les valeurs du DHT11.
Plusieurs solutions que j'ai déjà testées::
-Changer de pin
-Vérifier les connexions
-Testé avec arduino UNO (et ça marche)
-Tenté un autre code
-Ne pas utiliser d'OLED et mettre un bip à un seuil
-Lire avec le moniteur série : pleins de caractères incompréhensibles d'affichent (peut être que c'est normal je n'ai jamais testé pour autre chose)
-Vérifier si mon Arduino pro mini pouvait recevoir et envoyer des données correctement (oui)
J'ai aussi cherché sur d'autres forum mais je n'ai pas trouvé la solution
Mon code (avec l'OLED et le RTC):
#include "SSD1306Ascii.h"
#include "SSD1306AsciiAvrI2c.h"
# include <dht11.h>
SSD1306AsciiAvrI2c display;
dht11 DHT11;
#define DHT11PIN 2
#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val){
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val){
return( (val/16*10) + (val%16) );
}
void setup()
{
Wire.begin();
Serial.begin(9600);
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
setDS3231time(30,41,15,6,04,01,20);
Serial.begin(115200);
display.begin(&Adafruit128x64, 0x3C);
display.setFont(System5x7);
}
void loop()
{
uint8_t chk = DHT11.read(DHT11PIN);
displayTime();
display.set1X();
display.setCursor(35, 5);
display.print(DHT11.temperature, DEC);
display.print("C ");
display.print(DHT11.humidity, DEC);
display.print("% ");
delay(500);
}
void printLeft(const char *s, int y, int size)
{
if (size == 1) {
display.set1X();
}
else {
display.set2X();
}
display.setCursor(0, y);
display.print(s);
}
void printRight(const char *s, int y, int size)
{
size_t w;
if (size == 1) {
display.set1X();
}
else {
display.set2X();
}
w = display.strWidth(s);
display.setCursor(display.displayWidth() - w, y);
display.print(s);
}
void printCentered(const char *s, int y, int size)
{
size_t w;
if (size == 1) {
display.set1X();
}
else {
display.set2X();
}
w = display.strWidth(s);
display.setCursor((display.displayWidth() - w) / 2, y);
display.print(s);
}
void printValue(int value, int y, int size)
{
char s[20];
size_t w;
sprintf(s, "VALUE: %d", value);
w = display.strWidth(s);
if (size == 1) {
display.set1X();
}
else {
display.set2X();
}
display.setCursor(0, y);
display.print(s);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year){
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year){
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime(){
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
// send it to the serial monitor
display.set2X();
display.setCursor(30, 1);
if (hour<10) {
display.print("0");
}
display.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
display.print(":");
if (minute<10){
display.print("0");
}
display.print(minute, DEC);
}