myRaspberry

Mes aventures en Raspberry et Arduino

DS1302 Module Horloge temps

Posted on 14 mai 2016  in Arduino, Composants

Nativement, votre Arduino ne gère pas l’heure, il faut donc lui intégrer un composant sur pile permettant de lui fournir cette fonctionnalité.

J’ai donc acheté le DS1302 sur amazon.fr à 1.33euros. (3 semaines de transport mais gratuit !)

41NoPMwvnSL._SX425_

Arduino pin: Pin (batterij aan de voorkant):
+5v 01 – VCC1 (+5v)
GND 02 – GND
D6 03 – CLK (serial clock)
D7 04 – DAT (data)
D8 05 – RST (reset)

Etape 1 : télécharger les bibliothèques.

Nous aurons besoins de

Ces librairies sont à importer dans votre client arduino (croquis/inclure librairie)

 

Etape 2 : mettre à l’heure votre horloge

Par défaut, le composant n’est pas initialisée, il faut donc lui affecter une heure, elle sera maintenue tant que la pile sera chargée. (assez longtemps..)

#include <DS1302.h>
 
// Init the DS1302
// DS1302 rtc([CE/RST], [I/O], [CLOCK]);
DS1302 rtc(8, 7, 6);
 
void setup() {
  // Set the clock to run-mode, and disable the write protection
  rtc.halt(false);
  rtc.writeProtect(false);
  
  // Setup Serial connection
  Serial.begin(9600);
 
  // The following lines can be commented out to use the values already stored in the DS1302
  rtc.setDOW(FRIDAY);        // Set Day-of-Week to FRIDAY
  rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(6, 8, 2010);   // Set the date to August 6th, 2010
}
 
void loop() {
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");
 
  // Send time
  Serial.println(rtc.getTimeStr());
  
  // Wait one second before repeating 🙂
  delay (1000);
}