Week 4

Embedded Programming

By Patricia Samudio Salinas โ€” on

Embedding Programming

We have reached week 4 of the Fab Academy (who would have thought I would be here ๐Ÿ˜…). Well, it's the week of embedded programming ๐Ÿคฏ๐Ÿ™ˆ. It's amazing what the world of microcontrollers, chips and connections is.

Learning about how small electronic/digital devices work is an interesting adventure, and here I share what I have learned so far. Let's go!

Micro-controllers

They are small programmable devices that are present in various devices. They consist of three functional units: central processing unit, memory, and input/output peripherals. Through the input and output pins, it is capable of executing commands stored in its memory.

Commonly with the following features:

  • Central processing unit -ranging from small and simple 4-bit processors to complex 32-bit or 64-bit processors.
  • Volatile memory (RAM) for data storage
  • ROM, EPROM, EEPROM or Flash memory for program and operating parameter storage.
  • Discrete input and output bits, allowing control or detection of the logic state of an individual package pin.
  • Serial input/output such as serial ports (UARTs)
  • other serial communications interfaces like IยฒC, Serial Peripheral Interface and Controller Area Network for system interconnect.
  • Peripherals such as timers, event counters, PWM generators, and watchdog.
  • Clock generator - often an oscillator for a quartz timing crystal, resonator or RC circuit.
  • Many include analog-to-digital converters, some include digital-to-analog converters.
  • In-circuit programming and in-circuit debugging support.
๐Ÿ“ My reading journey began with the materials for the week, as well as the specific article on Microcontroller in Wikipedia.

Raspberry Pi Pico microcontroller. Source: Vishnu Mohanan en Unsplash

Programming languajes

In my limited programming experience ๐Ÿ˜ฐ, I must say that this week was the opportunity to explore more about other programming languages that I had already heard of. That is the case of the C++ language.

For my inquiries about it, I rely on the W3S page that has a very simple language to teach a variety of programming languages.

Micropython, On my part, it is more familiar to me since I had some introduction to Python before, whether in bootcamps or online courses. And thanks to the review of the documentation, I understand that it is based on the C language. ๐Ÿคฏ


Arduino UNO

At our Fab Lab, we have two microcontrollers available: Arduino UNO and Raspberry Pi Pico. In this section, I share my learning and practice with Arduino UNO.

Arduino UNO micro-controller. Source: Own photograph

Some of the features

  • High performance, low power AVRยฎ 8-bit microcontroller.
  • Operating voltage: 2.7V to 5.5V for ATmega328P.

Pinout of Arduino. Source: https://store-usa.arduino.cc/products/arduino-uno-rev3


Practice with Arduino UNO

As a first experience with Arduino, I used the Wokwi simulator. Here, I am sharing my experience. Firstly, I wrote the code using programming materials, in this case, C++.

Code

                                // the setup function runs once when you press reset or power the board
                                void setup() {
                                // inicializar el pin pin LED_BUILTIN digital como un output.
                                pinMode(LED_BUILTIN, OUTPUT);
                                }

                                // the loop function runs over and over again forever
                                void loop() {
                                digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
                                delay(1500);                      // wait for a second
                                digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
                                delay(1500);                      // wait for a second
                                }
                            

Then I run the Code, and the light is connected!!! I like this

Made with Clipchamp

Blink LED_BUILTIN Simulation - Arduino UNO - on Wokwi.

Then, I added an external LED and a resistor to the simulator.

Based on the blink code for the internal LED, I replaced some data: Before the line void setup(), the corresponding pin for the LED (pin 13) is declared. Then, in the pinMode() and digitalWrite -high and low- lines, we can indicate the number 13 that we assigned to the LED. I made the connections: Pin 13 to the anode and the GND pin to the cathode. ๐Ÿคฉ Awesome!!

Code

                                    int LED = 13;
                                    void setup() {
                                    // put your setup code here, to run once:
                                    pinMode(13, OUTPUT);
                                    }
                                    
                                    void loop() {
                                    // put your main code here, to run repeatedly:
                                    digitalWrite(13, HIGH);  // turn the LED on (HIGH is the voltage level)
                                    delay(1500);                      // wait for a second
                                    digitalWrite(13, LOW);   // turn the LED off by making the voltage LOW
                                    delay(1000);                      // wait for a second
                                    }
                                
Made with Clipchamp

Blink LED OUTPUT Simulation - Arduino UNO - on Wokwi.

A second practice in the Wokwi simulator with Arduino was a classic instruction programming: print โ€˜Hello worldโ€™. For then, I was a start a new project Arduino UNO from Scratch. I write a code for the โ€˜Hello worldโ€™, and โ€˜voilรกโ€™โ€ฆ in the virtual terminal was print the phrasse ๐Ÿ˜Ž.

With our fellows, we named the controllers with compound names, as a way to have fun and get fond of the microcontrollers ๐Ÿ˜Š. Now, Arduino UNO is 'Arduardo' ๐Ÿฅธ and my Raspberry Pi Pico is โ€˜Raul Ricardo Picoโ€™ ๐Ÿ˜Ž.

Code

                                    void setup() {
                                        // put your setup code here, to run once:
                                        Serial.begin(9600);
                                        Serial.println("Hello Arduardo!");
                                      }
                                      
                                      void loop() {
                                        // put your main code here, to run repeatedly:                                
                                      }                                      
                                

For the external communication test, I used the Arduino IDE and a USB type B connection.ardware with a USB conection. In the Arduino IDE, I pasted the same code written in Wokwi, uploaded it, and then observed that the light is on ๐Ÿคฉ.

Blink code

Hello 'Arduardo' code

Video 'blink' reply.

Raspberry Pi Pico

In the site Raspberry.com, we was read about the Raspberry Pi Pico, that is a range of tiny, fast, and versatile boards built using RP2040, the flagship microcontroller chip designed by Raspberry Pi in the UK. Programmable in C and MicroPython, Pico is adaptable to a vast range of applications and skill levels, and getting started is as easy as dragging and dropping a file.

Raspberry Pi Pico micro-controller. Source: Own photograph

Some of the features

  • Dual ARM Cortex-M0+ @ 133MHz.
  • Power supply for digital GPIOs, nominal voltage 1.8V to 3.3V.

Pinout of Raspberry Pi Pico. Source:https://datasheets.raspberrypi.com/pico/Pico-R3-A4-Pinout.pdf

Another piece of information that I found interesting about Raspberry, is that the post-fix numeral on RP2040 comes from the following:

  1. Number of processor cores (2)
  2. Loosely which type of processor (M0+)
  3. floor(log2(ram / 16k))
  4. floor(log2(nonvolatile / 16k)) or 0 if no onboard nonvolatile storage

Why is the chip called RP2040?. Source: https://www.raspberrypi.com/documentation/microcontrollers/rp2040.html


Practice with Raspberry Pi Pico

For the RP Pi Pico practice, I used the Wokwi simulator and Thonny IDE again for peripheral communication. Here is my experience:

On Wokwi, I started a new project, this time using the MicroPython on Pi Pico option. Then, I started writing the 'Blink' code first, and then 'Hello World'. Both were successful ๐Ÿ˜Ž

Code Blink

                                        from machine import Pin, Timer
                                        led = Pin(25, Pin.OUT)
                                        timer = Timer()

                                        def blink(timer):
                                            led.toggle()

                                        timer.init(freq=2.5, mode=Timer.PERIODIC, callback=blink)
                                    

Then I run the Code, and the light is connected!!! I like this

Blink Simulation - Raspberry Pi Pico - on Wokwi.

Here, I used the same code, in Thonny IDE

An here, the reply in RP hardware, connected with a micro USB:

Code Hello World

                                            print("Hello, Raul Pico!")
                                        
Code used in Wokwi simulator

                                            import utime

                                            while True:
                                                print('Hello, Raul Ricardo Pico');
                                                utime.sleep(1.5);
                                        
Code used in Thonny IDE

Then I run the Code, and the light is connected!!! I like this

Hello World Simulation - Raspberry Pi Pico - on Wokwi.

And a 'Hello world' reply in a virtual terminal of Thonny IDE:


What I learned

๐Ÿคฏ The complex or simplicity of the programming languajes.

๐Ÿ˜ณ What is a Micro-controllers and its applications.

๐Ÿ˜ณ Diferences with sitaxis of languajes C and Python (MicroPython).

How I learned it

By the guidance of the local instructors and colleagues ๐Ÿ™Œ๐Ÿผ

Through the lectures of micro-controllers datasheets

Through wikis and blogs of programming languages

What I should avoid

๐Ÿ™ˆ I have trouble staying focused on one subject because I am easily distracted by my curiosity and tend to switch to other topics.

What I should do

Continue the lecture, practicing anad exploring especific codes for my project.

To learn more about microcontrollers to choose the right one for the project.