Embedded Programming

Objectives for Week 4



Group Assignment

Group Assignment Link.

Raspberry Pi Pico

For this week's assignments, I selected the Raspberry Pi Pico as my development board and used Wokwi to simulate the program I wrote. I experimented with MicroPython for programming.

Image taken from Mouser Electronics.

RP2040

The Raspberry Pi Pico is powered by the RP2040 microcontroller, a dual-core Arm Cortex-M0+chip developed by Raspberry Pi. I referred to the RP2040 datasheet to gain a basic understanding of the chip.

Specification

Image taken from Cytron Marketplace.

The datasheet of RP2040 can be downloaded from here.

A system overview of RP2040 chip.
Raspberry pi pico pin out.

Wokwi

This week's assignment involves programming a board and simulating its functionality using a simulation software, eliminating the need for a physical board. One popular tool for this task is Wokwi, an online simulator for embedded systems. Wokwi supports various microcontrollers, including Arduino, ESP32, ESP8266, and RISC-V, allowing users to write and test code directly in the browser. It provides a visual interface to connect components like LEDs, sensors, LCD displays, and motors, enabling real-time simulation of embedded projects. Additionally, Wokwi allows users to debug their code, analyze serial output, and adjust circuit connections without requiring any hardware.


Creating a New Project

  1. I searched for Wokwi in my browser and clicked on the first link that appeared. After signing in, I selected "My Projects" and started a New Project.


  2. Then, I selected Raspberry Pi Pico and chose the MicroPython template for the simulation.



Programming

MicroPython is used to program the Raspberry Pi Pico, and the simulation is performed in Wokwi, eliminating the need for any physical components.

I experimented with some basic examples to understand the fundamentals of programming.

1. Hello World


import time
time.sleep(.2)
print("Hello World!")

Explanation


2. Blink Onboard LED


from machine import Pin
from utime import sleep 
led = Pin(25,Pin.OUT) #define on board LED 
while True: 
    led.toggle() #toggle state of LED 
    sleep(1) #delay for 1 second

Explanation


3. External LED and Push Button

In the previous two programs, I used only the Raspberry Pi Pico to control the onboard LED. Now, I have connected an external LED, a resistor, and a push button to the Pi Pico to control the LED using the button. The resistor is used to limit the current flow to the LED, preventing damage.

The components can be inserted from the + icon.

Taken reference from here.

I attempted to modify the code I obtained from the above link. Comparing it with the Blink Onboard LED code, I removed the "if-else" loop from the original code and rewrote it using an "if" condition and "led.toggle()". To my surprise, the code worked perfectly, and I got the expected output.

The push button is connected between GPIO0 and GND. The positive terminal of the LED is connected to GPIO1, while the negative terminal is connected to GND. A resistor is placed in series with the LED to limit the current flow.

from machine import Pin
from utime import sleep
button = Pin(0, Pin.IN, Pin.PULL_UP)
led = Pin(1,Pin.OUT) #define on board LED
while True:
    print(button.value())
    if button.value()==0: 
        led.toggle() #toggle state of LED
        sleep(1) #delay for 1 second

Explanation


4. External LED and Ultrasonic Sensor

I used an LED and an ultrasonic sensor to measure distance. If the sensor detects a value of 200 cm or less, the LED starts blinking. Otherwise, the LED remains off. Refered the Connection of Ultrasonic Sensor to Raspberry Pi Pico and the Code from here.

I took the code from the above link and wired the circuit as mentioned in the documentation. I simulate in Wokwi and the code works perfectly.


Then I imported an LED and a resistor and wired it like the External LED and Push Button project. I wnt to turn ON the LED when the distance is 200cm or below and turn it OFF when the value is above 200cm. When refering the code while recieved for External LED and Push Button I saw if-else loop is used for turning ON and OFF led. So I tried "if-else" loop and I got the output.


from machine import Pin, time_pulse_us
import time

SOUND_SPEED=340 #Speed of Sound in air.
TRIG_PULSE_DURATION_US=10

trig_pin = Pin(15, Pin.OUT) # GP15 pin of the Pico
echo_pin = Pin(14, Pin.IN)  # GP14 pin of the Pico
led = Pin(1, Pin.OUT)
while True:

    # Prepare the signal
    trig_pin.value(0)
    >time.sleep_us(5)
    # Create a 10 µs pulse
    trig_pin.value(1)
    time.sleep_us(TRIG_PULSE_DURATION_US)
    trig_pin.value(0)

    ultrason_duration = time_pulse_us(echo_pin, 1, 30000) # Returns the propagation time of the wave (in µs)
    distance_cm = SOUND_SPEED * ultrason_duration / 20000

    print(f"Distance : {distance_cm} cm")
    time.sleep_ms(500)
    if distance_cm <=200:
        led.value(1)
    else:
        led.value(0)

Explanation


Hero Shot

As a beginner in programming, writing code was initially challenging. However, after experimenting with various programs, I successfully turned on an LED based on input values from an ultrasonic sensor. Seeing the program run correctly was a highly satisfying experience.



Conclusion

This week, I learned about microcontrollers and processors, along with how to program them. I focused on the RP2040 microcontroller, explored its datasheet, and examined its specifications. Additionally, I experimented with the Wokwi simulator for circuit wiring, programming, and simulation. For coding, I primarily used MicroPython and worked with the Raspberry Pi Pico as my main microprocessor.


ChatGPT is used for paraphrasing.

References


Downloads