================================================================================================================================================

Sunday, January 9, 2022

Raspberry Pi Pico - A Tiny Power-Packed Development Board

 




Raspberry Pi Pico is a relatively new, tiny but powerful development board designed by the Raspberry Pi (UK), which is based upon RP2040 microcontroller chip, developed in-house by Raspberry Pi. The microcontroller has dual-core Arm Cortex- M0+ Processor, running at clock up to 133MHz, with 264KB internal RAM and supports up to 16MB of external Flash. Multicore operation is supported by a pair of four entry FIFOs (one in each direction between the cores).

The Pi Pico board has 2MB of on-board QSPI Flash, which is programmable through USB Mass Storage Device mode or using Serial Wire Debug port. It also features wide range of flexible I/Os, like GPIO, UART, I2C, SPI, USB and also Programmable I/O (PIO), which is unique to this board. The board has small footprint with solderable headers. It is very cost-effective, available from large number of vendors, may be available in your nearest electronic shop, too. This opens doors for utilization towards a wide range of applications for novices as well as experts.

Detailed information on Raspberry Pi Pico is available here:

Following image with Pin-diagram of Pi Pico gives idea about various I/Os of the device: (courtesy- Raspberry Pi website):


The Pi Pico can be programmed in C/C++ (SDK support is provided by Raspberry Pi), Arduino or MicroPython. A lot of resources have already been made available in such a short time, for this board. That actually shows the rapid growth of interest in Pi Pico and usage by hobbyists/ professionals together. New libraries, packages and platform supports are getting added at quite a fast pace.

I've liked this board so much that I'm literally hooked to it 😊. The purpose of my this post is just to put up an introduction with resource pointers, which I wanted to do for quite some time, before I start posting experimental codes also, using it.


The Pico-SDK: This is official SDK from Raspberry Pi team, for writing C/C++ or assembly language code for Pi Pico, full with headers, libraries and build system, designed to provide an API and programming environment. Multicore support examples are also given.

Pico-SDK GitHub page provides detailed description and links for documentation for using the SDK projects. The SDK can be easily setup with VS Code for programming the Pi Pico. It also has the example projects to demonstrate the use of SDK for various peripherals/ interfaces of the device.


The Pico-Arduino: The addition of the Arduino support has made Pi Pico even more easier to use for any newcomer who wants to get hands-on experience with it. 

There are two packages, shown in Arduino Boards Manager, "Arduino Mbed OS RP2040 Boards" and "Raspberry Pi Pico/RP2040" as shown here:



Both these board support Pico-SDK functions also. Raspberry Pi Pico/RP2040 GitHub page shows detailed information on installation and usage of the package with Arduino. Arduino Pico Document  provides detailed information on writing the code, with examples. This document by Earle F. Philhower, III, is getting updated, like, every other day! :)  This board also supports multicore programming using additional setup() and loop() functions (named setup1() and loop1()).

The  Arduino Mbed OS RP2040 Boards package supports Pico with Mbed RTOS for Arduino. This is really powerful. The Mbed OS is already being used widely in ARM Cortex devices, and support for the same in Pico Arduino makes things even more versatile and mighty by providing access to the various Mbed APIs inside Arduino.

With PlatformIO for VS Code: PlatformIO is a powerful IDE, which can be used for Pi Pico Arduino. The "Raspberry Pi Pico" board is shown in the Board Explorer of PlatformIO home menu. This is same as "Arduino Mbed OS RP2040 Boards" in Arduino IDE. The platfomIO provides professional features of code editing, auto code-completion suggestions / intelliSense, debug options, etc., which are not available currently in Arduino IDE.

Both the above Arduino packages have different default pin assignments for I2C, SPI, etc. Hence, interchangeability of the code has to be properly checked before changing the board setting. As lot of work is currently going on, I suppose there will be a single combined package in the future, which will do away with some of the initial confusion while programming.


Pi Pico with MicroPython: Pi Pico can also be used with MicroPython (or CircuitPython) interpreters, which make programming still easier for newbies who want to avid C/C++, or for programmers already experienced in Python.

Micropython.org page for Pi-Pico provides quick-start reference on installation and usage of micropython on Pi Pico. Also, Raspberry Pi official Datasheet of Python SDK is filled with detailed info on coding with micropython, with examples. This GitHub page also provides ready to use examples in micropython.

Thonny is easy to use, light-weight Python IDE for beginners. The Raspberry Pi page here provides getting started with micropython using Thonny IDE. The Adafruit page provides details on configuring Thonny with CircuitPython for Pi Pico.


Supporting Base Boards: Raspberry Pi Pico is easy to use on breadboard itself for initial prototyping or testing work, great for hobbyists. But if you need some proto board with ready connections for experiments, the Maker Pi Pico Base board looks great. It has got LEDs already mounted on each GPIO pin, providing visual feedback on the code functioning. It has also got other peripherals like microSD card, buzzer (speaker), audio jack, pushbuttons and also connector for ESP32 device for adding WiFi to the project. It's also available online, I think at most places. Here is my board:


You can also opt for a simple GPIO extension boards, like these:



These board are available on amazon, needs to just search for Pi Pico. You'll also get these or similar easily from your local suppliers, too. (In my recent experience during the chip shortage period, I've found getting Pi Pico boards far easier than many other development boards. That's one more incentive to use them!)

Summary:

I hope this gives you the idea of how easy it is to get started with Pi Pico board. The board is really cute, beautifully manufactured! Once you hold it in your hand, may be you'll fall in love with it, too!!😊 Small and yet, it is so powerful for such a size! Go ahead and grab one, you won't regret it.! 👍


Further Reading/ References:

  1. Raspberry Pi Pico Getting Started (Intro to setting up development environment, programming and loading the code into the board)
  2. RP2040 Microcontroller Datasheet (device datasheet of the chip itself)
  3. Pi Pico Board Datasheet (Pico board details with electrical, mechanical specs, powering, components, schematics and application briefs)
  4. Pi Pico Pinout (Pinout with alternative functions)
  5. Pi Pico Hardware Design Reference (Must read before you go ahead with making more hardware connections, other than blinking LEDs!)
  6. Pi Pico C/C++ SDK (C/C++ API function/interface details)
  7. Pi Pico Python SDK  (Python API function/interface details)
  8. Pico-SDK Documentation (doxygen)
  9. Arduino Pico documentation (details of Arduino API implementation)
  10. MicroPython Quick Reference for Pi Pico
  11. Beginner's Guide for R-Pi Pico (A nice starting guide with hardware setup and programming in MicroPython, from seeedstudio.com)


Wednesday, July 4, 2018

UART or SD Card based 8-Channel Data-logger with temperature log




Hi Friends,
        here is one more data-logger circuit, which will be useful for some who have not much ventured into the ADC for datalogging or RTC or i2c communication, etc. This one is based on ATmega32. It has 8-channel 10-bit ADC. Hence, providing us with 8-channels with 0 to 5V voltage level measurement. The circuit also has on-board RTC (based on DS1307) to log the data with time-stamp. Data is logged via UART or using microSD card. The circuit has 3-pin UART connector, which can be used to connect to PC/laptop using UART(TTL) to USB serial converter, commonly available in electronic markets (UART setting: 38400-1-N-1).
      Also, there is a thermometer IC, DS1621, which is connected via i2c (along with the RTC IC) and provides temperature measurement from -55 to +125 Deg. C. The temperature is also sent along with the voltages to the UART for logging.
     (The logging with microSD card is added later in this post).
    The circuit schematic is given in the following figure (pdf file can be downloaded from here):


     At power on, the current date and time are displayed on the first raw of LCD. The temperature and the logging ON/OF status is displayed on the second raw. A green LED is also turned ON as an additional status of the proper power-up of the microcontroller .
     The data measurement can be started anytime by pressing the 'SET' button (keep pressed till the red LED turns ON). The recording ON status is displayed on LCD as well as by the glowing red LED. During this recording, at a regular interval (as defined in the code), the microcontroller gets the temperature from DS1621, measures the 8 voltages connected to 8 of its screw-terminals, w.r.t GND on the 9th terminal (as shown in the schematic), and forms a string which includes line number, time (hh:mm:ss format), temperature and the 8 voltage values, in a comma separated format. This string is then sent to UART, which can be logged into a file or seen on a terminal program in the PC/laptop using the serial-to-usb converter.
     The operation is shown in the following screen-shot of the Proteus simulation (in the simulation, the temperature set in the IC and displayed by the LCD has difference of 1.5C, as the code applies the correction factors read from the IC to the temperature reading and then displays it, which represents the actual temperature):


     (Note: The Proteus simulation files are included in the source code folder download at the end of the post).
   
     The circuit in operation, with logging ON and OFF, is shown the following pics:




        The actual UART output captured on a terminal of the Atmel Studio during the logging ON,  is shown in the left side image, where temperature shown is 30.3 C, channel-2 was connected with +5V (VCC) and channel-7 was connected with the Li battery cell of the RTC. Rest of the channels were kept open at the screw-terminals.

      The RTC date and time can be changed using the three push-buttons, similar to the procedure shown in my previous post of the RTC based relay-control. The procedure is given here:








 

For setting RTC Date/Time:

  1. Press 'SET' button and Power ON the circuit, keeping the button pressed while the circuit is starting.
  2. "RTC Setting.." message will be displayed on the first row of the LCD.
  3.  Release the 'SET' button "Date: XX" will be displayed on the second row of the LCD, where XX is the existing current date as per the RTC.
  4.  Press 'UP' or 'DOWN' button to increase or decrease the Date. When desired date is displayed, press 'SET' button to store it
  5. "Month: XX" message will be displayed, where XX is the current month as per the RTC
  6. Press 'UP' or 'DOWN' button to increase or decrease the Month. When desired month is displayed, press 'SET' button to store it
  7. "Year: XXXX" will be displayed, use 'UP'/ 'DOWN' buttons to change the year and then press 'SET' button to store the year value
  8. Then "Hour: XX" will be displayed, set it as per the previous steps and also set next "Minutes: XX" similarly, and store using 'SET' button.
  9. When the Minutes is set, "RTC Setting" mode is over and normal operation resumes, where the LCD will display Date and time in the first row and temperature and log:OFF status in the second row.

 

  • Data-logging with SD card 

         (Updated: Sep 2021)



Above image shows the logging with microSD card. A commonly available microSD card module (mostly used with Arduino) is used here. Please note that this module interferes with programming of the board on AVRISP port (which is not a problem with Arduino as it uses the USB bootloader programming and not AVRISP), so it's necessary to remove the card while loading the code. You can find solutions/modules online to overcome this issue.

Following images show the generated .csv file in the SD card and also opened file in MS-Excel. The logging was done without any voltages connected to the ADC channels, so the voltages are 0, but the  temperature log can be seen along with the time stamp. You can view another log file here, where I've connected the 8 channels to 3V RTC battery and VCC (~5V) line one by one (cross-coupling of signal to nearby free channel is also logged there).


The file name format is: DDMMYYNN.CSV, where DDMMYY is the date of logging and NN is the file number. After power on, the file number is set to 00. Every time logging is started and then stopped, the file number increments, hence, new file is created. As file number is a 2 digit value, max 100 (00 to 99) log files can be created after power on in a day, after that it will send error message. I guess you may not need to do the logging ON and OFF more than 100 times a day (after single power ON), but if you do, the file name string can be tweaks as necessary.

After Power off and on, when datalogging is started, the code looks if the filename is already existing or not, if it is, it will change the log-file number (and hence, name) to next available number, and so on. So, always a new file is created whenever logging is started.

  • SD card datalogging using FatFS library

I'm  also uploading here the data-logging project code with a more generic FatFs library, which has been highly standardized and is officially supported/included by many IDEs/ Development boards/ Platforms (like Arduino, mbed, STM32, Keil, etc..). I've just replaced my FAT32 (and SD routines) libraries with FatFs modules. This also has been tested and the project folder is included in the downloads (Sr.No.2).

The datalogging and RTC setting procedures are same for all the above options, as explained in the earlier paragraphs. Functionality or user operations are same, only internal library change is there. Data-logging option with only UART or only SD card or both can be selected by proper macro selection in the main.h file. 

Note: You can connect UART while operating if you need to see the status/error messages for any troubleshooting. Otherwise, UART is not necessary if logging is done in SD card.

The circuit hardware is not much complicated, can be assembled on a general purpose PCB also. I'm including here the Gerber files also, along with the other downloads, if it's required.


Downloads:
  1. Source Code for SD/UART Logging (with FAT32 Lib) (Atmel Studio-7 project)
  2. Source Code for SD/UART Logging (with FatFs Lib) (Atmel Studio-7 project)
  3. Proteus Simulation for UART datalogging
  4. EAGLE Schematic and Board files
  5. Gerber Files
Datasheets: ATmega32,   DS1307,   DS1621
 
You may also check out my post here, where I started on SD cards, for more info/ further reading/ references.

Enjoy!!

Regards,

CC Dharmani
ccd@dharmanitech.com

Saturday, June 23, 2018

Relay-Timer Circuit with RTC (for auto-operating appliances) using AtmelStudio or Arduino




Hi friends,
     it's been years since I put up a new post here. So, starting again with a simple circuit, based on ATmega8, might be useful for newbies in AVR, RTC, Relay, etc.This is RTC based relay operating circuit to automatically power ON/OFF any electrical device/appliance at a preset time. 
    The necessity behind it was this: I had started taking a 230V AC electrical Tiffin (lunch) box to office a few months back, which takes minimum 30 minutes to very slowly heat up the food (it's kept slow heating as the idea is to just heat the food without re-cooking it). That means it's necessary to turn the box ON 30 minutes before lunch-time. And it happened quite a lot of time that I simply forgot or could not power on due to a meeting in another building. So, one day I quickly cooked up this circuit as I was already having all the required components (including an ATmega8 development board and a relay module, to minimize the soldering job).
    Before anything further, if you try to make this circuit, WARNING!! High Voltages are involved here, extreme care is necessary while testing/operation. Ensure proper insulation for the high-voltage lines.
    This circuit can be used for powering any electrical device by setting start and stop time, which is stored in EEPROM of ATmega8, so no need to set again upon power off. RTC (based on DS1307) is used for similar reasons, no need to keep the circuit ON when not necessary, or during power off, it will maintain the date/time running (with the help of the common 3v Li button-cell). RTC setting or start/stop time setting can be changed anytime using three push-buttons provided onboard.
    Now, the main components: The microcontroller is AVR ATmega8, small enough and with sufficient flash for the code required here. Initially (before making the custom-made PCB shown in the above pics), I used a off-the-shelf available 28-pin AVR Development board from Extreme Electronics. The board is shown here:

    Using such a board avoids soldering the IC base, power supply components, ISP connector, etc. And it's having some user area for soldering, which was sufficient for this project.
    The Dual-channel Relay board used initially is from EmbeddedMarket.com:


    This module is having two independent relays with transistor circuits for activation. The VCC supply line of the coils here are connected with 9V or 12 V DC (as per the DC adapter used to power the development board) and the AVR port lines are connected to RL1 and RL2 terminals. Two devices can be independently operated with this module by connecting the AC live input to the NO (Normally Open) terminal of the Relay and taking the output from C (common) terminal to the electrical device live input. The return line of the electrical device can directly go from device to the mains, without connecting to this unit. (Note: As I was having only one device to control, I passed the Phase (Live) through one relay and Neutral through the another relay. The program operates both the relays simultaneously to power the electrical device, i.e.Tiffin-box in this case).
    Here is the Proteus simulation screen-shot (during simulation, LEDs are used in place of the relays to check the functionality and the serial terminal is unused):


 
  Following is the simulation using ATmega328 (Arduino):


 (The Proteus simulation files are included in the source code download at the end of the post).

    As shown, upon powering on, after displaying a welcome message, the LCD displays current date and time, and the output status (via two relays). The output ON is also indicated using a Red LED on the circuit. The terminal (UART) in the circuit is kept extra, in case someone wants to use terminal instead of LCD and push-buttons to display/set timings (This further reduces the hardware onboard, it will add only a UART-USB converter module for PC/laptop connection, which are quite commonly available).
    The program reads the set-time in EEPROM by checking for a valid EEPROM signature. If time is not set, it sets default time of 1230 HRS for start time and 1300 HRS for stop time. Whenever a new time is set, it's stored in the EEPROM and used during operation thereafter.
    The quickly made proto of the circuit with whatever came handy is shown here (underneath the LCD are the AVR and relay boards with the rest of the circuitry):



       The 230V lines are properly screwed and totally insulated using glue-gun inside the box, including the relay terminals, so that not even a single strand of the wire comes out of the insulation.
     Of course, the circuit can be totally tested (or time can be set/ changed) without connecting the 230V lines, as we can confirm the continuity of the relay line by checking with multimeter. The high voltage line is necessary only during the actual operation.
    Later on, the schematic and layout of the PCB were designed using KiCad (latest version 5.0), as KiCad has recently become a really great tool (and free, too!), as compared to my quite earlier experience with it. I instantly liked it even better then the EAGLE lite, which I had been using so far.
The schematic is given here (when using Arduino, replace ATmega8 with ATmega328 in the circuit, in the same PCB):


       The PDF schematic can be downloaded from here.

The operation of the circuit is described here:

Circuit Operation:
--------------------------------------------
For setting RTC Date/Time:
  1. Press 'SET' button and Power ON the circuit, keeping the button pressed while the circuit is starting.
  2. "RTC Setting.." message will be displayed on the first row of the LCD.
  3.  Release the 'SET' button "Date: XX" will be displayed on the second row of the LCD, where XX is the existing current date as per the RTC.
  4.  Press 'UP' or 'DOWN' button to increase or decrease the Date. When desired date is displayed, press 'SET' button to store it
  5. "Month: XX" message will be displayed, where XX is the current month as per the RTC
  6. Press 'UP' or 'DOWN' button to increase or decrease the Month. When desired month is displayed, press 'SET' button to store it
  7. "Year: XXXX" will be displayed, use 'UP'/ 'DOWN' buttons to change the year and then press 'SET' button to store the year value
  8. Then "Hour: XX" will be displayed, set it as per the previous steps and also set next "Minutes: XX" similarly, and store using 'SET' button.
  9. When the Minutes is set, "RTC Setting" mode is over and normal operation resumes, where the LCD will display Date and time in the first row and O/P: ON/OFF status in the second row.
 Normal Operation:
  1.  When the circuit is powered ON without pressing the 'SET' button, it displays "Welcome, -Relay timer-" message for two seconds and then directly enters into the normal mode
  2. During the normal operation, the LCD display will be as per the above step 9. The Date and time are continuously updated on the display every second
  3. In the normal operation, whenever the RTC time matches with the Start Time, the Relays are energized, connecting the output lines with the respective input lines. 
  4. When the output is ON, as soon as the RTC time matches with the Stop time, the relays are turned OFF, cutting off the output
  5. The output ON and OFF are displayed on the LCD as well as indicated by Red LED ON and OFF, respectively
Setting Start/ Stop time:
  1.  Start and Stop time can be changed anytime  during the normal operation
  2.  Pressing the 'SET' button during normal operation displays currently set values of Start and Stop (End) times in "S:HH:MM E:HH:MM" format in the second row, where 'S' indicates Start time and 'E' indicates End (Stop) time. 'HH' and 'MM' are hours and minutes, respectively. The first row continues to display current date and time.
  3. During step 2, if 'SET' button is pressed again, display goes back to normal operation. But if 'UP' or 'DOWN' button is pressed instead, the program enters into the Start/Stop time setting mode, where "Start Hour: XX" is displayed in the second row.
  4. Use the 'UP'/'DOWN' and 'SET' keys to adjust Start Hour, followed by Start Min (Minutes), then Stop Hour and Stop Min. Each value is stored by pressing the 'SET' button.
  5. After the Step 4, the new values are stored in EEPROM and normal operation resumes.
       Here are two more views of the operational unit:
     


 Notes:
  • After RTC and Start/stop times are set, no intervention is required for circuit operation
  • Whenever necessary, the circuit can be switched off, the date/time settings will be maintained
  • If the circuit power is OFF and the start time is missed before the power is ON, the circuit will not set the o/p even if the Stop time is still quite away. It will set the o/p only when the next start time match with RTC. (This can be changed by minor modification in the code, if necessary).
  • Warning again, proper insulation and extreme care from high voltage line is must!! Never keep you high voltage outlet pins 'Male' type, always use socket type. Use 'male' types only for the high voltage 'inlet' pins.
    So, for last couple of months, I connect the Tiffin with the circuit (first thing after reaching office), using standard 230V AC plug, and power ON the circuit, check the display for normal operation and then continue with my work. The Tiffin gets ON and OFF as per the set time before lunch. At lunch time, I turn off the power (coz it's not necessary till next morning) and enjoy the hot meal!! The power can be kept ON also, if desired. For example, in case if you want to water your plants periodically by turning on the electrical motor, whether you are available nearby or not.

Here is the complete source code in AtmelStudio7 and Arduino project files along with the Proteus simulation files (please ensure programming the fuse bites for proper clock frequency values). When using Arduino, ATmega8 needs to be replaced with ATmega328:

Downloads

Source Code (AtmelStudio) and Simulation Download (ATmega8, @8 MHz internal clock)

Source Code (Arduino) and Simulation Download (ATmega328, @16 MHz External clock)

KiCad Project Schematic, PCB and Gerber files


Datasheets/ Links:   ATmega8,   ATmega328,   DS1307,   Arduino Website


Enjoy!!

Thanks & Regards,
CC Dharmani 
--------------------------------