Am reluat testele cu statia meteo si ceas care prezinta datele pe un afisaj cu rezolutie de 32x8 leduri (compus din 4 afisaje 8x8 leduri rosii), contorlate de integrate MAX7219, de data asta am afisajele montate intr-o carcasa, iar ca placa de dezvoltare, am folosit un Arduino Mega, ca era disponibil. Pentru temperatura se foloseste un senzor DHT11, iar pentru temperatura un DS18B20.
In articolul precedent am prezentat modululele de afisare din 8x8 leduri controlate de MAX7219, folosind librariile Ledcontrol si apoi MaxMatrix. Am realizat o varianta de ceas cu date meteo cu 4 module de afisare, la care se regleaza automat nivelul intensitatii luminoase a afisajului in functie de mediu si am folosit urmatoarele: - modul cu DS3231 pentru partea de ceas si data (functioneaza fara modificari si DS1307) - senzor DHT22 pentru temperatura si umiditate - fotorezistenta montata intr-un divizor rezisitiv Montajul arata asa:
iar schema folosita a fost:
In timpul experimentelor, am tot alimentat montajul ba din mufa usb, ba direct pe pinul +5V si GND si am observat ca uneori apar probleme la partea de ceas, aparand date eronate, asa ca am cautat solutii hard si soft si m-am oprit la una soft, de "curatire" a magistralei i2c, dupa cum am gasit in articolul Reliable Startup for I2C Battery Backed RTC Why the Arduino Wire library is not enough:
/** * I2C_ClearBus * (http://www.forward.com.au/pfod/ArduinoProgramming/I2C_ClearBus/index.html) * (c)2014 Forward Computing and Control Pty. Ltd. * NSW Australia, www.forward.com.au * This code may be freely used for both private and commerical use */
#include <Wire.h>
/** * This routine turns off the I2C bus and clears it * on return SCA and SCL pins are tri-state inputs. * You need to call Wire.begin() after this to re-enable I2C * This routine does NOT use the Wire library at all. * * returns 0 if bus cleared * 1 if SCL held low. * 2 if SDA held low by slave clock stretch for > 2sec * 3 if SDA held low after 20 clocks. */int I2C_ClearBus() {
TWCR &= ~(_BV(TWEN)); //Disable the Atmel 2-Wire interface so we can control the SDA and SCL pins directlypinMode(SDA, INPUT_PULLUP); // Make SDA (data) and SCL (clock) pins Inputs with pullup.pinMode(SCL, INPUT_PULLUP);
delay(2500); // Wait 2.5 secs. This is strictly only necessary on the first power// up of the DS3231 module to allow it to initialize properly,// but is also assists in reliable programming of FioV3 boards as it gives the// IDE a chance to start uploaded the program// before existing sketch confuses the IDE by sending Serial data.boolean SCL_LOW = (digitalRead(SCL) == LOW); // Check is SCL is Low.if (SCL_LOW) { //If it is held low Arduno cannot become the I2C master. return 1; //I2C bus error. Could not clear SCL clock line held low
}
boolean SDA_LOW = (digitalRead(SDA) == LOW); // vi. Check SDA input.int clockCount = 20; // > 2x9 clockwhile (SDA_LOW && (clockCount > 0)) { // vii. If SDA is Low,
clockCount--;
// Note: I2C bus is open collector so do NOT drive SCL or SDA high.pinMode(SCL, INPUT); // release SCL pullup so that when made output it will be LOWpinMode(SCL, OUTPUT); // then clock SCL LowdelayMicroseconds(10); // for >5uSpinMode(SCL, INPUT); // release SCL LOWpinMode(SCL, INPUT_PULLUP); // turn on pullup resistors again// do not force high as slave may be holding it low for clock stretching.delayMicroseconds(10); // for >5uS// The >5uS is so that even the slowest I2C devices are handled.
SCL_LOW = (digitalRead(SCL) == LOW); // Check if SCL is Low.int counter = 20;
while (SCL_LOW && (counter > 0)) { // loop waiting for SCL to become High only wait 2sec.
counter--;
delay(100);
SCL_LOW = (digitalRead(SCL) == LOW);
}
if (SCL_LOW) { // still low after 2 sec errorreturn 2; // I2C bus error. Could not clear. SCL clock line held low by slave clock stretch for >2sec
}
SDA_LOW = (digitalRead(SDA) == LOW); // and check SDA input again and loop
}
if (SDA_LOW) { // still lowreturn 3; // I2C bus error. Could not clear. SDA data line held low
}
// else pull SDA line low for Start or Repeated StartpinMode(SDA, INPUT); // remove pullup.pinMode(SDA, OUTPUT); // and then make it LOW i.e. send an I2C Start or Repeated start control.// When there is only one I2C master a Start or Repeat Start has the same function as a Stop and clears the bus./// A Repeat Start is a Start occurring after a Start with no intervening Stop.delayMicroseconds(10); // wait >5uSpinMode(SDA, INPUT); // remove output lowpinMode(SDA, INPUT_PULLUP); // and make SDA high i.e. send I2C STOP control.delayMicroseconds(10); // x. wait >5uSpinMode(SDA, INPUT); // and reset pins as tri-state inputs which is the default state on resetpinMode(SCL, INPUT);
return 0; // all ok
}
voidsetup() {
// put your setup code here, to run once:Serial.begin(9600);
int rtn = I2C_ClearBus(); // clear the I2C bus first before calling Wire.begin()if (rtn != 0) {
Serial.println(F("I2C bus error. Could not clear"));
if (rtn == 1) {
Serial.println(F("SCL clock line held low"));
} elseif (rtn == 2) {
Serial.println(F("SCL clock line held low by slave clock stretch"));
} elseif (rtn == 3) {
Serial.println(F("SDA data line held low"));
}
} else { // bus clear// re-enable Wire// now can start Wire Arduino masterWire.begin();
}
Serial.println("setup finished");
}
voidloop() {
// put your main code here, to run repeatedly:// do Wire RTC stuff here.
}
Sketch-ul meu, adaptat acestei solutii este maxmatrix_DS3231_clear_DHT22_4digit_ver6m0.ino !!! Ulterior, m-am gandit ca in locul senzorului DHT22 sa folosesc un sensor DHT11 pentru date de umiditate, avand in vedere ca precizia este suficienta, iar pentru temperatura sa folosesc un senzor DS18B20. Pentru a folosi doar un DHT11 in locul lui DHT22, se fac modificari minore in sketch:
in
Daca se doreste si o temperatura cu precizie buna, se poate folosi partea de citirea unui senzor DS18B20 cum e prezentata in materialul de la http://arduinoprojects.ru/ (in care pinul de date este la D8) si astfel sketch-ul devine maxmatrix_ds18b20_dht_4module_ver7m0.ino. Schema se modifica foarte putin, dupa cum se observa: