Skip to content

Week 4: Embedded Programming

Task: Embedded Programming

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

Learn more about Arduino IDE by checking our Groupsite

Individual assignment:
    Browse through the datasheet for a microcontroller
    Write and test a program for an embedded system using a microcontroller to interact (with local input &/or output devices) and communicate (with remote wired or wireless connections)

RP2040 Zero

Hello and welcome to week 4! This week we are learning Embedded Programming. My goal was to write a program that can turn a light on or off with the push of a button.

This is my very first foray into programming! I don’t know what most of the words people are using mean!

The microcontroller that I am using is called “RP2040 Zero”. You can view its datasheet here.

First you must download an IDE,Integrated Development Environment, that allows you to program a microcontroller. I used Thonny which is an IDE that uses the programming language Python.

At this core, computers are very dumb. Their language is composed of only 0’s and 1’s. A computer and computer program will only do exactly what it is told. It is up to you, the programmer, to figure out how to communicate to the CPU.

Luckily, someone much smarter had gone through the process of creating a Programming Language, this allows you to type using a certain syntax or patter, that then get interpreted into those 1’s and 0’s. We are so blessed to have access to this cumulative knowledge, and for FREE!

Thonny

thonny homepage picture

Download Thonny

image of rp2040

Next, plug in your rp2040. 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 (RP2040)” 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

Let there be light: Programming the LED

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. “machine.Pin” and “machine.pin” will be read as two entirely separate things.

picture of imports

Next we are going import some libraries! Libraries are pieces of code that have already been written. This makes the process of coding much faster as some of it has already been done for us.

Fun Tip: You can access the libraries by ctrl + click on them!

For those of you that are low vision (me). I will write out the grey text in the photo above. Some words will be changed. Notice how different parts of code are in different colors! If you want to write a comment that doesn’t affect your code, simply type a # in the front.

import machine #give us access to control pins import neopixel #give us access neopixel objects, the LED on the rp2040 is a neopixel *import time #gives us access to the time.sleep function.

“import” is the command we are giving. “machine”, “neopixel”, and “time.sleep” are the names of the libraries we are importing.

You may notice the words “neopixel objects” in the above explanation. An “object” is something that we can assign and refer to in code that will always follow the same rules.

first line of code Next up is this fancy line of code. I will break this down piece by piece.

*Pixel_pin = machine.Pin(16, machine.Pin.OUT)

Pixel_pin is the name of an object

the symbol = means that we are assigning meaning to that object.

machine.Pin means we are referring to the machine library to access a pin.

(16, machine.Pin.OUT) is us telling information about what we want the pin we are accessing to do. the 16 is the pin number. The LED we want to control is on pin 16. we know this by accessing the datasheet. machine.Pin.OUT mean that we are saying that the pin will be outputting information.

PLEASE keep in mind that CAPITALIZATION MATTERS!

num pixels

next is * num_pixels = 1

num stands for number, and we are saying that there is only 1 led.

now let’s get real fancy

*np = neopixel.NeoPixel(pixel_pin, num_pixels)

“np =” we are defining what the letters np mean

“neopixel.NeoPixel” we are accessing the neopixel library and using the object called “Neo.Pixel”. Notice how the capitalizations of their letters makes them different inputs.

“(pixel_pin, num_pixels)” The object NeoPixel needs to have defined the LED’s pin, if its an output, and how many of them there are. Notice how we are using the term we have already assigned!

while True

The goal is to create a blinking light. I use this bit of code.

“while True: “is how we start a loop! please keep in min that only the T is capitalized and you need a colon. Also, formatting matters! you hit “tab” to indent the next few line of code

the 0 in np[0] is the LED. There is only 1, but we start with the number 0. It is the first in the chain of 1.

Next you see 3 numbers. these represent the Red, Green, and Blue, or RGB Values. Inside the LED there are 3 tiny lights, red, green and blue. These lights can change in intensity from the numbers 0-255. Please keep in mind we are using Additive Colors, so if the values of all 3 are the same, they make white.

np.write() means that we are saving the values

time.sleep(1) DOES NOT determine how long the light stays off, but how long it will stay on. The datasheet says the rp2040 measures in seconds.

I repeat these three lines and play with the values to get different colors.

If you typed in all the code correctly, it should work! if it doesn’t:

check your spelling check the capitalization check the indentations check that you used the correct punctuation

Usually the shell will give an error and a hint to what line may be wrong. This is very helpful :)

breadboard

Bonus points! Here’s the code that makes the light slowly switch between 2 colors!

Programming a button

ALRIGHT BABY ITS TIME TO TRY CONTROL A LIGHT WITH A BUTTON

breadboard

First, you connect your RP2040 to a breadboard.

Next I plugged it into my computer.

Let’s test and see if the breadboard is getting power!

I used this circuit and BURNED OUT my LED. It made a hissing sound, was glowing red in the center, and smelled like plastic. All bad signs.

I contacted my instructor to ee what I did wrong. I thought I had short-circuited the poor thing.

Turns out my circuit was fine! I just needed a resistor. I was giving the LED WAY too much power and that’s why it burned out.

resistor equation

Graphic from Rhopoint Components

We used the equation V = I * R

If I understood it better I would explain it. But I don’t. I know how to use Ohm’s Law but I still haven’t figured out what it means.

Just know the RP2040 outputs 5V and that LED’s use 2V and I used a 330 Omega resistor.

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()

Cats

Echo and Shadow

I have an exciting update about Echo (right) and Shadow (left)! They have decided to HANG OUT on the ground floor with us! I never expected them to actually lay down while we were in the studio.

Files

DOWNLOAD PYTHON CODE HERE

Copyright 2026 - Creative Commons Attribution Non Commercial

Source code hosted at gitlab.fabcloud.org