4. Embedded Programming
4. Embedded Programming
Assignments
Here you can find a recording of the lecture from the 12th of february.
Here you can find a recording of the recitation from the 10th of february.
This week's assignments and learning outcomes, see here:
Group assignment:
- Demonstrate and compare the toolchains and development workflows for available embedded architectures
- Document your work to the group work page and reflect on your individual page what you learned
Individual assignment:
- Browse through the datasheet for your microcontroller
- Write a program for a microcontroller, and simulate its operation, to interact (with local input &/or output devices) and communicate (with remote wired or wireless connection)
Questions to be answered/from Nueval:
Have you...
- Linked to the group assignment page
- Browsed and documented some information from your microcontroller's datasheet
- Programmed your simulated board to interact and communicate
- Described the programming process(es) you used
- Included your source code
- Included ‘hero shot(s)’
Hero shots
Hero shots from group projects
ATtiny412 with Arduino IDE
ATtiny412 with Arduino IDE
Arduino Uno with PlatformIO
Arduino Uno with PlatformIO
Arduino Uno with Arduino IDE
Arduino Uno with Arduino IDE
Flowchart to compare two toolchains
Hero shots from individual assignments
Wokwi simulation with Arduino Uno
Arduino Uno with Arduino IDE
Wokwi simulation with Raspberry Pi Pico
Wokwi - Raspberry Pi Pico
LED on Raspberry Pi with Thonny
Inbuilt LED on Raspberry Pi Pico toggled on and off
Summary
This week, as a group, we investigated, explained and compared different toolchains and development workflows for embedded architectures. Here you can see the documentation on our group page. I was responsible for the ATtiny412 and the Arduino Uno part of the group work. Some of the problems I encountered I documented on the group page but in the beginning I got into trouble when making tables and I documented that here on my own page, because it was not exactly about the group assignment.
My individual assignment was to Write a program for a microcontroller. Then I simulatet the operation I chose to work with and made it interact with output devices and communicate through a wired connection.
Work process detail
Learning outcome from the group assignments
Learning how to create tables
When I was wondering how the best setup for comparizon would look like I saw the group project site at Fab lab Reykjavík here. It inspired us in the group to make a comparison table and I thought that this was a good oppertunity to learn how to make tables. I googled and found this site. I began by adding -tables to the Markdown Extensions in mkdocs.yml. Then I copied the code here and tried to adjust it to my information.
| Method | Description |
| ----------- | ------------------------------------ |
| `GET` | :material-check: Fetch resource |
| `PUT` | :material-check-all: Update resource |
| `DELETE` | :material-close: Delete resource |
One extra line wrecked the code
This was a failure because I added an extra line above the -------- to reduce the width and it wrecked the code as you can see here below.
| Microcontroller | Architecture | Number | Number of | Number of | Clock speed | Flash |
| | | of pins | digital pins | analog pins | | memory |
| ------------------- | ------------ | ------- | ------------ | ----------- | ----------- | ------ |
| ATtiny412
| | | | | | |
| ESP32-C3
| | | | | | |
| RP2040 XIAO
| | | | | | |
| | | | | | | | | | | |
This worked
Without the extra line everything worked as you can see in table 2.
Microcontroller | I/O Capabilities | Connectivity | Programming languages | Peripherals |
---|---|---|---|---|
ATtiny412 |
||||
ESP32-C3 |
||||
Rp2040 XIAO |
||||
---|---|---|---|---|
When I asked Svavar Konráðsson, he told me that I could use a table generator on this site. That simplified making the table quite a lot. Then you write into the columns on the site, click on Generate and then Copy and paste into your repository. Much easier and we used this method.
Other learning outcomes from group assignments
- Now I have a better understanding on how microconrollers work and got to know examples on how they differ.
- I got a better understanding on what a Toolchain is.
- I had some experience on how to use Arduino Uno prior to this assignments, but I this week I learned how to use the PlatformIO to program a microcontrollers.
- Jóhannes Andrésson taught me a few things. He told me that by writing the name of a code, f.ex. C++, after the comma's before the code then the code would appear in different colours and that is so much nicer than the simple grey code. He also told me that by adding def_list to markdown extensions, you could change the looks of words so that they appear with a light grey box as background. See more about definition lists here.
Group project file
Code for ATtiny412 and Arduino Uno
The individual assignments
Choosing a microcontroller
It has taken quite some time to decide how to make the final project. Now that I know I'm going to use irregular tracks of LED's, I was wondering if I needed a pin for each LED. Svavar explained to me that I could use Neopixel LED's and that they could form an irregular line. He also explained that Raspberry Pi has many pins, so I could use it if I wanted to, but he also told me that I could connect the Neopixel LED's could all be connected to one pin. I am going to see if Raspberry Pi Pico might be a convenient microcomputer for the project.
Raspberry Pi Pico
Datasheet and pinout for Raspberry Pi Pico
You can find information on Raspberry Pi Pico here. The screenshot below is from this information site.
Datasheet for Raspberry Pi Pico here.
Some of the key features are:
-
RP2040 microcontroller - 2MB Flash memory
-
Micro_USB B port that is used for power and data
-
40 pins that are 21x51 'DIP' style 1mm thick PCB with 0.a" through-hole pins (with edge castellations)
-
26 of those are multi-function 3.3V with a General purpose I/O (GPIO)
-
23 of those pins are digital
-
3 pins are ADC capable
-
-
3-pin ARM Serial Wire Debug (SWD) port
-
264kB multi-bank high performance SRAM
-
External Quad-SPI Flast with eXecute in Place (XIP) and 16kB on-chip cache.
-
Dual-core cortex MO+ at up to 133MHz
-
12-bit 500ksps Analogur to Digital Converter (ADC)
-
Peripherals:
-
2xUART, 2 x 12C, 2 x SPI, 16 x PWM channels
-
Timer with 4 alarms, Real Time Counter.
-
-
It is possible to use Pico as a surface mountable module or for souldering 0.1" pin-headers.
Thonny
Thonny is a platform that can be used for programming Raspberry Pi Pico with the Python language.
Installing Thonny
I followed the directions from Nicolas Decoster here on how to install Thonny. I began by downloading Thonny here. According to this page Python 3.10 is built in Thonny so you don't have to use a separate Python installation, unless you want to. I also watched this video that shows step by step how to setup Raspberry Pi Pico.
Choosing an Interpreter
You have to choose an interpreter. First you click on Run and then you select Interpreter. As you can see, I chose MicroPython (Raspberry Pi Pico). Then I clicked on Install or update MicroPython. You have to be patient because it takes time to show and process the update. When Thonny was ready I clicked on Install.
Seeing all files and also if Raspberry Pi Pico is connected
I clicked on View and Files and then all files can be seen on the left side, both files on the computer and files on my Raspberry Pi Pico. In the bottom window I could see that the Raspberrry Pi Pico was connectoed.
Simulation
Wokwi
In this week's lecture and recitation we were introduced to the online editor Wokwi. Wokwi is an online Electronics simulator, as you can read about here. It has many interesting features such as Wifi simulation and Visual Studio Code Integration. I began by checking out the website and wonder what I should do. Then I decided to try out the Arduino simulator.
Arduino simulation in Wokwi
On the front page you choose between different microcontrollers and boards.
How Wokwi works
This photo show what the simulator looks like. On the left you write your code. On the right you choose which elements you want to use, like LED's, resistors, buttons and all kinds of elements. Here I simulated how 5 LEDs would blink. I decided to use breadboard in this simulation, just to see how it would look. I found directions in a video on an Icelandic site. You can see it here. In the video he uses only 4 LEDs, but I increased it to 5 LEDs. First I had to define which pins I was using and as you can see, I used pins number 8, 7, 6, 5 and 4. Then under Void Loop I had to define all commands; when a LED should be lit and when it should turn off. By using the HIGH command it turns the LED on. Only one LED is turned on each time and the other ones are told to be turned off by using the command LOW. The command DELAY says that this situation will vary for the time written in the brackets. He only uses 200 in delay but I used 1000, which equals 1 second.
Videos not playing
I made a mistake in the beginning and wrote information on pinMode above the Void setup. Wokwi sent a warning and said that the build failed. After investigating the failure I saw that I had to move the text under Void setup and then the simulation worked when I pushed the Play button.
Wokwi-Arduino
Arduino simulation in Wokwi - files
Arduino Blinking 5 LEDs_Wokwi_Json
Arduino Blinking 5 LEDs - Wokwi - sketch
Arduino Blinking 5 LEDs - Wokwi - text
OBS Studio
I made this video with OBS Studio and you can read about it here. I was using it for the first time and a co-teacher of mine showed me how to use it. He showed me that I could choose "hotkeys" from the keyboard to start, pause and end a recording. I chose S, Space and R as hotkeys, but that was a mess. I left the OBS program open while documenting today's work and in the evening I found many unexpected videos in my folder! The OBS Studio program recorded many things I did today without me knowing, f.ex. a phonecall I made and the music I was listening to. So, no hotkeys for me!
Hotkeys in OBS studio
When NeoPixels will be used in final project
I found these directions that I want to look closer at when it comes to connecting the NeoPixels.
I also foundinformation on single LED'S here.
Raspberry Pi Pico simulation in Wokwi
I want to be able to connect irregular tracks of Neopixel LED's to Raspberry Pi Pico in my final project. The plan is to show how a whale travels from one place to another by lighting one LED at a time so that it looks as if it's moving on a map. For this reason I wanted to simulate a row of LED's blinking in a row in Wokwi. My first problem was to figure out the right way to connect a Neopixel LED to the Pico board. The next problem to solve would be to make a connection between a row of LED's. The third problem to solve would be to create a code that can control the LED'S. The fourth problem was to find information on how to connect and program one single NeoPixel LED! I searched and didn't find wat I wanted, but I found one example on how to simulate with one Neopixel LED, but it didn't work, and another one with a NeoPixel ring. I decided to see if I could learn from these examples.
Connecting the LED in Wokwi
LED
I needed an explanation on what VSS, VDD, DIN and DOUT meant and I found simple explanations here.
LED
This image shows what the NeoPixel compatible led (WS2812) looks like in Wokwi.
LED VSS
VSS should be connected to ground, so I did that. VDD: The LED is powered here with positive supply voltage. I connected it to the V-bus pin on the Raspberry Pi Pico.
LED DIN and DOUT
The data input from the microcontroller. I connected it to Pin number 1. DOUT is for Data output, so I did not connect it.
Programming the LED in Wokwi
Example code - NeoPixel LED and RPP
I found an example code with a single LED and Raspberry Pi Pico in Wokwi here. When I clicked on Play, nothing happened, so I was interested to know what was not working. I compared it to another code I found on Wokwi here that worked when you clicked on Play, but it was for a NeoPixel ring, not single LED's.
I decided to copy the code from here to see if the code would work with a single LED, even though the original code was for a NeoPixel ring. I was sure that the beginning of the code would at least work for my LED, because I found it rational. The beginning was:
The last numbers meaning: (1 = , 0 = current state machine, 2 = Pico:GP1 or Pin nr.1).
Can't import name Neopixel
Well, this did not work. I was surprised to get an error in this line, because this worked for the NeoPixel ring.
Then I decided to search for another code to try out. I found this code here.
I changed the number of Neopixels from 10 to 1 and the Pin number from 0 to 1. That was not enough and the code did not work.
MicroPython code not looking as nice as C++
Code looks bad
I have been using three comma's (''') before and after C++ codes I put on this site. When I add C++ after the upper comma's, the code looks so nice. When I do the same with MicroPython codes, the text looks irregular and not nice. I haven't found the right way to fix this, but I am documenting it here and hopefully I will find the solution. For now I skip the comma's.
The right type of comma
I thought I was using the right type of comma's but Jóhannes Andrésson told me to try making a comma by using ALT GR and +. He also told me to write only py but before I had been using MicroPython. This worked! See example below.
The right way to use the comma and py
This is what it looks like
Files for Neopixel ring
Files for NeoPixel ring that I tested on one NeoPixel LED
I used this code I found on Wokwi here that was for a NeoPixel ring, but I wanted to see if it would work on a single LED. Here are the files that I copied, changed and used.
Json file with code for NeoPixel ring
Files for faulty code/Neopixel LED
Files for NeoPixel ring that I tested on one NeoPixel LED
Here are the files I copied, changed and used for a single LED and Raspberry Pi Pico in Wokwi here. The original did not work and it did not work in my project either. I could not find what was wrong.
Project txt file for faulty code
An example that worked!
I told Svavar that I couldn't find a good example and he pointed out an example that from Nicolas Decoster here had made on how to simulate with a single LED and a Raspberry Pi Pico. I connected the LED the same way that he did and saw that he connected the DIN to Pin12 and the VDD to Pin11. I thought that the VDD would have to be connected to the V-bus, but it was obviously possible to connect the VDD to a Pin.
Wokwi interface - Neopixel LED and RPP
Here you can see the interface of Wokwi. You can see the Raspberry Pi Pico, the NeoPixel LED and a part of the code.
Wokwi interface
Closeup of NeoPixel LED
Here you can see how I connected the NeoPixel LED according to how here connected his example.
A closeup of the connections between the NeoPixel LED and the RPP
Files for Wokwi-Raspberry Pi Pico and NeoPixel LED
Wokwi files for Raspberry Pi Pico and NeoPixel LED
Here are the files for one NeoPixel LED connected to Raspberry Pi Pico. LED is programmed to change colours. The settings were done as in the example from Nicolas Decoster and the code was copied from him.
Wokwi file - NeoPixel LED and RPP
Wokwi json file - NeoPixel LED and RPP
Wokwi txt file - NeoPixel LED and RPP
Video of LED switching between colours
Wokwi - Raspberry Pi Pico
To toggle a LED on and off
The last thing I tried was to follow simple directions that Svavar Konráðsson sent me. You can see the directions in icelandic here:
Inbuilt LED on Raspberry Pi Pico toggled on and off
Learning outcome for individual project
-
I learned how to use Wokwi and I think it is very useful
-
I learned the basics in using Thongi, but I know I have a lot more to learn.
-
I learned the basics in programming the Raspberry Pi Pico, but I haven't got full understanding yet and therefore the experiments in simulation for NeoPixel single LED did not work, until I used a premade code from Nicolas Decoster
-
I learned the basics about how to connect a NeoPixel LED
-
I learned the basics on how to use OBS studio
-
Last, but not least, I learned to make a MicroPython code block.