4. Embedded Programming¶
Goals¶
group assignment:
- [x] demonstrate and compare the toolchains and development workflows for available embedded architectures
individual assignment:
- [x] browse through the data sheet for your microcontroller
- [x] write a program for a microcontroller,
- [x] simulate its operation, to interact (with local input &/or output devices)
- [x] and communicate (with remote wired or wireless connections)
- [x] extra credit: test it on a development board
- [] extra credit: try different languages &/or development environments
Tools Used¶
TLDR; Nice images¶
Group Project¶
For my group project I tested the tool chain of Thonny IDE from programing in MicroPython on a ESP32-C3 from Xiao
My writeup can be found on our nodes site along with the rest of our groups exploration
Individual Project¶
1. Wokwi Sim¶
My goal for this week is to implement a wokwi MicroPython program that uses a joystick and some buttons
1.0 Basic Wokwi test from recitation¶
Below is a sample of the first bit of code that came from the recitation on programing and debugging. python
It imports the machine and time modules, creates a variable led and sets it to the output of pin 0 on a rp2040.
Then it sets a while
loop that is always set to True (so it is essentially infinite).
contained in this while loop is a toggle for the led followed by a command to wait for half a second
import machine
import time
led = machine.Pin(0,machine.Pin.OUT)
while True:
led.toggle()
time.sleep(0.5)
print("Hello, Pi Pico!")
1.05 RTFM¶
In the spirit of spiral development i would like to set have the option to make my project wireless down the road while maintaining the ability to work wired early on to help remove complexity. From talking to my instructor and from what i have seen in the global lecture the ESP32 platform seems like a good fit for my project.
We have a Xiao ESp32-C3 in stock in our lab so this is my starting point. Seeed studios documentation is found on this page and is very easy to navigate.
This is along side a more traditional datasheet that can be downloaded from there site in PDF form.
This document goes into more detail into the chip and the board, detailing its architecture.
But it also contains useful, more qualitative information such as suggested use cases for the MCU.
To conclude while i don’t really understand much of the details of this document yet, having it as something i can reference later in the project will be invaluable. Key details such as the number of inputs and outputs i can use, memory requirements and power efficiency will all need to be reverenced back here if i am to continue with this chip.
1.1 Asking the robots¶
For my project I will likely be using ESP32 platform. As I’m still learning I asked chatGPT for a bit of help to get started.
The first prompt was
how to program for and get code onto an esp, I use a mac and would like to use mircopython, what ide's, plugins, library would you recommend
This gave me some useful info but the core prompt that got me started was
i am doing some testing and simulation in wokwi, can you help me plan the componants I would need to create a spacemouse style computer interface for 3D modeling software using simple off the shelf componants, for now a simple joystick interface is ok
From this I got the following code;
I then asked for it to comment the code for a complete beginner to help me understand what was happening.
# Import necessary modules from MicroPython's machine library
from machine import Pin, ADC
import time # Import time module to add delays
# Set up the X-axis joystick input
# ADC (Analog-to-Digital Converter) reads an analog voltage (0V - 3.3V)
# and converts it to a digital value (0 - 4095 on ESP32)
x_axis = ADC(Pin(34)) # Connect joystick X output to GPIO 34
# Set up the Y-axis joystick input (same as X but for vertical movement)
y_axis = ADC(Pin(35)) # Connect joystick Y output to GPIO 35
# Set up the joystick button as a digital input
# PULL_UP means the button defaults to "1" (HIGH) when not pressed
# When pressed, it connects to ground (0 or LOW)
button = Pin(13, Pin.IN, Pin.PULL_UP) # Connect joystick button to GPIO 13
# Infinite loop to continuously read joystick and button values
while True:
x = x_axis.read() # Read the X-axis analog value (0 - 4095)
y = y_axis.read() # Read the Y-axis analog value (0 - 4095)
btn = button.value() # Read button state (1 = not pressed, 0 = pressed)
# Print the values to the console
# This helps visualize joystick movements and button presses
print(f"X: {x}, Y: {y}, Button: {btn}")
# Small delay (100ms) to slow down loop execution
# Without this, the ESP32 would flood the console with too many messages
time.sleep(0.1)
With this code I was ready to set up my Wokwi test environment.
1.2 Digital analog¶
I am using Wokwi to learn and test my code as it removes a lot of additional complexity early on.
Below I will be making my test for a ESP32 reading basic inputs from a joystick.
I use the blue plus button at the top of the circuit side of Wokwi to import a new component, in this case a analog joystick
Once I have brought it in I connect the outputs from the joystick to the pins on the ESP32 as per my program. Making sure not to forget ground.
This is all the ‘hardware’ I need for my test.
Run the test with the green play button and we can see the output in the console from the print()
command.
1.3 Making it my own¶
Now that I have got the AI generate code up and running I want to make it my own. Instead of the digital value for each input being listed on the screen every 0.1 seconds I would like the output to say something more readable. (and change the frequency to every 1 sec for slow pokes like me)
Left
Right
Up
Down
Neutral
After the print out I added a new line and made an if statement
if x > 2048:
print("Left!")
elif x < 2048:
print("Right!")
My goal here is to have the console print Left!
when the stick is moved left and Right!
when moved right.
Sucess!
So I added the same again for the y axis
if y > 2048:
print("Up!")
elif y < 2048:
print("Down!")
And I wanted to know when the joystick was not being used so for this I used a and
operation to check two values in an if statement
if x == 2048 and y == 2048:
print("Controler is neutral")
Complete test!
Link if you want to try it yourself!
Also Link to my chatGPT conversation
In Summary¶
I feel Wokwi was an invaluable tool for me to pick up this week. Being able to sandbox systems and code in an environment isolated from the messy details of physically putting the board together is very useful. I still have a lot of system design to do for my final project so using a tool like this can help me iterate more quickly.
Things I would do differently next time¶
As always time was a factor this week so I mostly stick with the fist thing I tried which was micro python and the ESP32 platform. In an ideal world it would be nice to explore more languages, in particular C as it seems to be very widely used.
See below link to to files created this week: