issue200:micro-ci_micro-la
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue200:micro-ci_micro-la [2023/12/30 10:42] – créée auntiee | issue200:micro-ci_micro-la [2024/01/04 15:28] (Version actuelle) – d52fr | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | **It’s about REAL Time | ||
+ | Greetings again, fellow Beings. Happy December and a better 2024 to you! | ||
+ | This month, we will look at real time clocks on our Microcontrollers and specifically on the RPi Pico/ | ||
- | It’s about REAL Time | + | All of the boards have some sort of time keeping capability, even if it doesn’t know what time it is when you power up the board. You can always set the date and time (grossly) in code at the start of your program and if your microcontroller is BlueTooth or WiFi capable, you should be able to connect to a server, grab the time and then do what is needed.** |
- | Greetings again, fellow Beings. | + | C'est du temps RÉEL |
- | This month, we will look at real time clocks on our Microcontrollers and specifically on the RPi Pico/ | + | Je vous salue à nouveau, chers amis. Joyeux mois de décembre et une meilleure année 2024 ! |
- | All of the boards have some sort of time keeping capability, even if it doesn’t know what time it is when you power up the board. You can always set the date and time (grossly) in code at the start of your program and if your microcontroller is BlueTooth or WiFi capable, you should be able to connect to a server, grab the time and then do what is needed. | + | Ce mois-ci, nous allons nous intéresser aux horloges en temps réel sur nos microcontrôleurs et plus particulièrement sur le RPi Pico/Pico-W. Alors que les cartes RPi Pico n'ont normalement pas de RTC (Real Time Controller - Contrôleur temps réel) intégré, de nombreuses cartes Pico tierces plus récentes en ont déjà un à bord. Même si vous utilisez une ESP-32 ou une ESP-8266, le processus général est à peu près le même pour toutes ces cartes, surtout si vous utilisez MicroPython. |
- | For today, we’ll skip that discussion, and move on to using a Real TIme Controller chip. There are many versions out there, but the one I’m going to use is the DS1307 RTC I2C module. | + | Toutes les cartes ont une certaine capacité à garder l' |
- | This is what mine looks like and as you can see, the left side has spots for 5 pins and the right side has spots for 7 pins. You can choose either side and for this project only use the Gnd, VCC, SDA and SCL pins. Since it’s I2C, it will work on almost any Microcontroller, | ||
- | Breadboarding it | + | **For today, we’ll skip that discussion, and move on to using a Real TIme Controller chip. There are many versions out there, but the one I’m going to use is the DS1307 RTC I2C module. At the date of this writing, I was able to find them as low as $0.88 USD in single quantities. Of course, the shipping on just one, would cost a TONNE, so look around locally and you will probably find one for less than $15.00 USD possibly with shipping included. |
- | I need to repeat that there are MANY different boards that use the DS1307, so I’m going to give generic instructions | + | This is what mine looks like and as you can see, the left side has spots for 5 pins and the right side has spots for 7 pins. You can choose either side and for this project only use the Gnd, VCC, SDA and SCL pins. Since it’s I2C, it will work on almost any Microcontroller, |
- | If you want to use I2C(0) on the pico, Pins 6 and 7 are the suggested pins. If on the other hand you want to use I2C(1), the suggested pins are 9 and 10. In any event, the SDA on the Pico connects to the SDA on the DS1307 and the SCL on the Pico connects to the SCL on the DS1307. The Head bone is also connected to the Neck bone and so on and so on. | + | Pour aujourd' |
- | Now we can get to our code. | + | Voici à quoi ressemble le mien et, comme vous pouvez le voir, le côté gauche a des emplacements pour 5 picots et le côté droit a des emplacements pour 7 picots. Vous pouvez choisir l'un ou l' |
- | The Code | ||
- | There is an added benefit in the code to include output to an OLED display using the SSD1366 I2C display. | + | **Breadboarding |
- | The DS1307 | + | I need to repeat that there are MANY different boards that use the DS1307, so I’m going to give generic instructions for the breadboard. Don’t try to follow |
- | Again, I’ve added the optional code for the OLED display. If you want to use it, just un-comment this part of the code and a few lines in the main while loop (top right). | + | If you want to use I2C(0) on the pico, Pins 6 and 7 are the suggested pins. If on the other hand you want to use I2C(1), the suggested pins are 9 and 10. In any event, |
- | While we can take the data directly from the rtc.datetime() function and the time.localtime() functions, it comes back as a tuple and if you just want to glance at the screen or display, it’s somewhat hard (at least for this old guy) to grab the correct information out of 8 values out of the tuples. | + | Now we can get to our code.** |
- | The rtc.datetime() function returns | + | Le maquettage |
+ | |||
+ | Je dois répéter qu'il y a BEAUCOUP de cartes différentes qui utilisent le DS1307, donc je vais donner des instructions génériques pour la plaque d' | ||
+ | |||
+ | Si vous voulez utiliser I2C(0) sur le pico, les broches 6 et 7 sont les broches suggérées. Si, par contre, vous voulez utiliser I2C(1), les broches suggérées sont les 9 et 10. Dans tous les cas, le SDA du Pico se connecte au SDA du DS1307 et le SCL du Pico se connecte au SCL du DS1307. « L'os de la tête est également connecté à l'os du cou et ainsi de suite. » | ||
+ | |||
+ | Nous pouvons maintenant passer à notre code. | ||
+ | |||
+ | |||
+ | **The Code | ||
+ | |||
+ | There is an added benefit in the code to include output to an OLED display using the SSD1366 I2C display. However, I’ve commented all of the code out, so you can run without it. | ||
+ | |||
+ | The DS1307 is (in this software) setup on I2C(0). If you want to include the OLED display, you could have it on the same I2C bus, but I’ve got the software to handle it on I2C(1). | ||
+ | |||
+ | Again, I’ve added the optional code for the OLED display. If you want to use it, just un-comment this part of the code and a few lines in the main while loop (top right). | ||
+ | |||
+ | While we can take the data directly from the rtc.datetime() function and the time.localtime() functions, it comes back as a tuple and if you just want to glance at the screen or display, it’s somewhat hard (at least for this old guy) to grab the correct information out of 8 values out of the tuples. ** | ||
+ | |||
+ | Le code | ||
+ | |||
+ | Il y a un bénéfice supplémentaire dans le code avec l' | ||
+ | |||
+ | Le DS1307 est (dans ce logiciel) configuré sur I2C(0). Si vous voulez inclure l' | ||
+ | |||
+ | Encore une fois, j'ai ajouté le code optionnel pour l' | ||
+ | |||
+ | Bien que nous puissions prendre les données directement des fonctions rtc.datetime() et time.localtime(), | ||
+ | |||
+ | |||
+ | **The rtc.datetime() function returns it’s values as follows… | ||
dt=(2023, 12, 9, 5, 9, 59, 41, 0) | dt=(2023, 12, 9, 5, 9, 59, 41, 0) | ||
Ligne 44: | Ligne 75: | ||
dt=(2023, 12, 9, 9, 59, 41, 5, 343) | dt=(2023, 12, 9, 9, 59, 41, 5, 343) | ||
- | Which at first glance seems to be the same format. | + | Which at first glance seems to be the same format. However, it’s not. It works out as… |
Year, Month, Date, Hour, Minutes, Seconds, Weekday and Day of the Year | Year, Month, Date, Hour, Minutes, Seconds, Weekday and Day of the Year | ||
Ligne 50: | Ligne 81: | ||
Local Time: 12/09/2023 - 09:59:41 | Local Time: 12/09/2023 - 09:59:41 | ||
- | So, taking all that into consideration, | + | So, taking all that into consideration, |
- | The first line in the function simply copies the incoming data from “d” to dtin, which makes more sense. | + | La fonction rtc.datetime() renvoie ses valeurs comme suit : |
+ | |||
+ | dt=(2023, 12, 9, 5, 9, 59, 41, 0) | ||
+ | |||
+ | Le tuple rtc.datetime est calculé comme suit : | ||
+ | |||
+ | Année, mois, date, jour de la semaine, heure, minutes, secondes et sous-secondes. | ||
+ | |||
+ | D' | ||
+ | |||
+ | dt=(2023, 12, 9, 9, 59, 41, 5, 343) | ||
+ | |||
+ | Ce qui, à première vue, semble être le même format. Mais ce n'est pas le cas. Le résultat est le suivant : | ||
+ | |||
+ | Année, mois, date, heure, minutes, secondes, jour de la semaine et jour de l' | ||
+ | |||
+ | Heure locale : 12/09/2023 - 09:59:41 | ||
+ | |||
+ | Donc, en considérant tout cela, j'ai créé une petite fonction qui prend le tuple de rtc.datetime() ou de time.localtime() et un second paramètre qui spécifie la source des données. 0 pour rtc.datetime et 1 pour time.localtime. Il renvoie deux valeurs : la date et l' | ||
+ | |||
+ | |||
+ | **The first line in the function simply copies the incoming data from “d” to dtin, which makes more sense. I also wanted to have a value that could be examined easily, rather than just name the parameter dtin. | ||
As you can tell, the year is handled the same, whether the data is coming from rtc or timer, so all we do here is step through of the first three items in the tuple, assign them to temporary values, then zero pad them on the left to make sure that 7 minutes after 11 shows like “11” “07” with whatever separator you decide you want. | As you can tell, the year is handled the same, whether the data is coming from rtc or timer, so all we do here is step through of the first three items in the tuple, assign them to temporary values, then zero pad them on the left to make sure that 7 minutes after 11 shows like “11” “07” with whatever separator you decide you want. | ||
- | Now we handle the time portion. | + | Now we handle the time portion. If we are dealing with the rtc data, we skip (for this project) the fourth item in the tuple (remember it’s zero based so it is actually designated as dtin[3]), since it’s the Day of Week. Then we pull each data item and, as we did with the dates, zero pad the item (top right). |
- | If, however, we are dealing with the time.localtime structure (a ‘1’ in the second parameter), we only need to pull in the next three items in the tuple and as above, we convert them into zero padded strings (bottom right). | + | If, however, we are dealing with the time.localtime structure (a ‘1’ in the second parameter), we only need to pull in the next three items in the tuple and as above, we convert them into zero padded strings (bottom right).** |
- | Now (top middle) is where we use the f-string feature to format the strings. | + | La première ligne de la fonction se contente de copier les données entrantes de « d » vers dtin, ce qui est plus logique. Je voulais aussi avoir une valeur qui puisse être examinée facilement, plutôt que de simplement nommer le paramètre dtin. |
+ | |||
+ | Comme vous pouvez le voir, l' | ||
+ | |||
+ | Nous nous occupons maintenant de la partie temporelle. Si nous traitons les données rtc, nous sautons (pour ce projet) le quatrième élément du tuple (rappelez-vous qu'il est basé sur zéro et qu'il est donc désigné comme dtin[3]), puisqu' | ||
+ | |||
+ | Toutefois, si nous avons affaire à la structure time.localtime (un « 1 » dans le deuxième paramètre), | ||
+ | |||
+ | |||
+ | **Now (top middle) is where we use the f-string feature to format the strings. So you can easily see that it would be simple to change the separators or move the items around. | ||
That’s it for the formatting function. | That’s it for the formatting function. | ||
- | Finally, we can code the main portion of the program. | + | Finally, we can code the main portion of the program. It’s really very simple. We instantiate the machine.RTC object as rtc. Then we get what the board thinks is the correct date and time. We then send that structure to the ConvertDT function and a 0, since the data is directly from the rtc object. Once we get it back, we print it out. Next we get the local time from the time module and send that to the ConvertDT function with a parameter of 1. Finally we start a loop, making a call to time.localtime, |
- | One other thing I should tell you about. | + | One other thing I should tell you about. If you use Thonny as your IDE, you might notice that for some reason whenever you check the time on your Pico through the IDE, it is correct. Why would that be?** |
- | From what I understand, whenever you connect a Microcontroller to Thonny, one of the first things that happens is that Thonny pulls the date/time information from the local computer and then sets the date/time of the Microcontroller for us. | + | Maintenant (en haut au milieu), nous utilisons la fonction f-string pour formater les chaînes. Vous pouvez donc facilement voir qu'il serait simple de changer les séparateurs ou de déplacer les éléments. |
- | Remember, however, if you connect your Microcontroller directly to power without connecting to your computer, the date and time probably won’t be anywhere close to reality. | + | C'est tout pour la fonction de formatage. |
+ | |||
+ | Enfin, nous pouvons coder la partie principale du programme. C'est vraiment très simple. Nous instancions l' | ||
+ | |||
+ | Il y a une autre chose que je dois vous dire. Si vous utilisez Thonny comme IDE, vous pouvez remarquer que pour une raison quelconque, chaque fois que vous vérifiez l' | ||
+ | |||
+ | |||
+ | **From what I understand, whenever you connect a Microcontroller to Thonny, one of the first things that happens is that Thonny pulls the date/time information from the local computer and then sets the date/time of the Microcontroller for us. | ||
+ | |||
+ | Remember, however, if you connect your Microcontroller directly to power without connecting to your computer, the date and time probably won’t be anywhere close to reality. At that point, you can call Doctor Who for a “wibbly wobbly timey wimey sort of thing”, or you can use a DS1307 RTC module. | ||
I’ve created a repository on Github for the code for this month at https:// | I’ve created a repository on Github for the code for this month at https:// | ||
- | Until next time, HAPPY NEW YEAR and, as always; stay safe, healthy, positive and creative. | + | Until next time, HAPPY NEW YEAR and, as always; stay safe, healthy, positive and creative.** |
+ | |||
+ | D' | ||
+ | |||
+ | Souvenez-vous cependant que si vous branchez votre microcontrôleur directement sur le secteur sans le connecter à votre ordinateur, la date et l' | ||
+ | |||
+ | J'ai créé un dépôt sur Github pour le code de ce mois à https:// | ||
+ | |||
+ | Jusqu' | ||
+ | |||
+ | |||
+ | // Traduction de la ligne noire de l' | ||
+ | **Here we set up the DS1307 on I2C(0).** | ||
+ | Ici, nous paramétrons le DS1307 sur I2C(0). |
issue200/micro-ci_micro-la.1703929336.txt.gz · Dernière modification : 2023/12/30 10:42 de auntiee