Skip to content

Week 5: Embedded Programming

group assignment: • demonstrate and compare the toolchains and development workflows for available embedded architectures

For our group assignment we began by each taking on a different embedded architectures…

Arduino IDE (Angela)

Using the Arduino IDE as the development environment, I started off with an Arduino Nano board, which uses the ATmega328P microcontroller. Then I went through the following steps with the guidance of my instructor:

  1. Downloaded Arduino IDE
  2. Walked through the ATmega328P datasheet to take a look under the hood and get an overview of its architecture, memory segments, and features
  3. Set up the Arduino Nano board with the breadboard and plugged into my computer using USB-C cable
  4. Perused some of the available example code under File>>Example menu
  5. Selected my port and board in the Arduino IDE interface
  6. Ran through simple practice programs, adding various components to the breadboard accordingly (ex. LED, potentiometer, resistor)

Programming Practice

Blink (with built-in LED)

For the first practice test, we used the example code available in the Arduino IDE for “blink” which turns an LED on for one second than off for one second repeatedly. I quickly found out that the very thorough documentation and notes within the code make it easy to understand what each line of code is doing. For example, pinMode(LED_BUILTIN, OUTPUT) signifies an initializing of the digital pin of the LED already built into the Nano board as an output. This means the subsequent code will apply to this pin.

After selecting the board and hitting upload, the LED on the board began to blink! By raising the value within the delay code line we could make the LED blink slower and vice versa.

blink code

Push button

Then we hooked up a button to the board, putting it in D5 (a digital port) since the button is a digital component. Using the button example code template, I set pin number for the button at 5 and I also ended up tweaking the setup so that the initial setup up of the button when not pressed has the LED on by adding “INPUT_PULLUP” in the code. Uploaded and the button worked.

button code

Dim light

Next, I learned how to go about dimming lights after hooking up an LED to the breadboard with the help of Addie. Since the LED is a digital component, it just turns on or off. To practice with dimming, I learned that we could use the blink code and experiment with the rate at which we blink the light to give the illusion that the light is dimming. After experimenting with the values input for the delay code which dictate how long between the LED turning on and off, I found a time that worked and the light appeared dimmer (even though in reality it is actually turning on and off at a rate faster than the human eye can detect).

dim code

dim pic

Dim light with potentiometer

After learning about the potentiometer component (an analog component) and how to set that up on the breadboard, my final challenge in class was to figure out code for how to dim the light using the potentiometer. I learned that the potentiometer has three legs – one to connect to power, one in the middle to connect to the analog pin which reads the variable voltage as you move the dial, and one to connect to ground and complete the circuit.

From my earlier practice, I know I would need to define the constant pin number where my LED light would be which I put at 5. I would use the code used to dim light but now needed to also inlcude other components in the set-up. After some instruction on initial set up for potentiometer code (setting the pin at A3 and initalizing serial communication at 9600 which essentially establishes a communication channel between the Arduino and computer for reading the variable inputs), I think added in the LED as an output. For the loop, I also learned about how to set up the potentiometer to so that it could be read and to print its reading. From there, I had to figure out what to write in the rest of the code so that the potentiometer input would correlate to the dimming of the light. After some trial and error I found out the I could use the printed “reading” within the delay code. Since the number for the reading goes as high as 1024, I ended up multiplying it by 15 (value used in the previous challenge) and dividing that by 1024. That worked pretty well!

dim analog

Lesson Learned

In experimenting with this workflow, some interested lessons learned:

  • If not pulled, pins wil float around which we don’t want. We want them to be pulled to a known state, either pull pins high (5V) or low (ground). You do this within the program code.
  • Do not forget semicolons! Just forgetting “;” will cause errors
  • code written in all capital letters indicates a constant
  • Define variables and constants in the code before the set up and loop functions
  • how to use a multimeter to measure voltage, current, and resistance. For example, when you don’t pull a pin you can use the multimeter and see that its “floating” do to the variance in voltage rather than staying constant.
  • analog vs digital - digital signal is something on or off (1 or 0) whereas an analog signal is something that will continuously vary. The Arduino nano board has a side of pins for analog and another side for digital.
  • how to use a potentiometer which is an analog, variable resistor

Thonny (Castor)

Downloading and setting up Thonny

thonny homepage picture

Download Thonny

image of rp2050

Next, plug in your rp2050. Hold down the reset button, then the boot button. Then release the reset button and the boot button in that order.

configure Interpreter

next in the bottom right corner click the button that says “no backend”. It may also say “Local Python 3”

thonny options

There are 2 drop down boxes. Select “MicroPython (RP2050)” and “Try to detect port automatically”

Then on the BOTTOM RIGHT corner, DO NOT hit “OK”. Instead, click “Install or update MicroPython”

net settings

There’s a few more dropdowns. Select “RPI-RP2” and “RP2” and “Raspberry Pi Pico/Pico H” and “1.27.0”

click instal

Then click Install

shell image

This next part is how to turn on the embedded LED. On Thonny, there are 2 main areas to type in. The upper part is where you write code. The lower part, the Shell, works as a terminal and also give you handy info if something in your code is not working.

Test to see if your microcontroller is working by typing EXACTLY

  • print (“Hello Friend”)

and hitting enter. You should get a response right below is with the words “Hello Friend”

Python is a case sensitive language. This means letters that are uppercase and lowercase are read as separate characters. “print” and “Print” will be read as two entirely separate things.

Programming a button

breadboard

First, you connect your RP2050 to a breadboard.

Next I plugged it into my computer.

fixed board fixed board

I also wired a button to Pin 7! It is separate from the wiring used for the LED. We want these circuits to be separate. I also changed the wiring to look more neat. The left and right boards are functionally the same board!

The following is the code. Anything with a # in front of it explains the purpose of that line.

code we used

import machine #Library that allows us to communicate with pins
import neopixel #Library that allows us to communicate with the embedded LED
import time #Library that allows us to set timeframes in seconds

pixel_pin = machine.Pin(16, machine.Pin.OUT) #using machine library to assign pin 16 as an output

Num_pixels = 1 #number of LEDs in this strip is 1

np = neopixel.NeoPixel(pixel_pin, Num_pixels) #assigning np by #accessing the neopixel library and creating an object called "Neo.Pixel".
#The object Neo.Pixel takes 2 parameters, the pin to which the Neo.Pixel is wired, and the number of pixels

button_pin = machine.Pin(7,machine.Pin.IN, machine.Pin.PULL_UP) #new line creating button pin input object


while True:                     #Creates an infinite loop
    while button_pin.value():   #Creates a second loop of what to do while the button pin is up

        np[0] = (0, 0, 100)     #sets the color of the LED
        time.sleep(.01)         #sets how long the light will sya in that state
        np.write()              #sets the code

        np[0] = (0, 0, 100)     #sets the color of the LED
        time.sleep(.01)         #sets how long the light will sya in that state
        np.write()

    np[0] = (255, 0, 0) 
    time.sleep(.1)
    np.write()