Skip to content

6. Embedded programming

Group assignment:

  • Browse your microcontroller’s datasheet
  • Compare performance and development workflows with other architectures of a multimeter and oscilloscope

To see our group assignment click here

Individual assignment:

  • Write a program for a microcontroller development board you’ve made, to interact (with local input and/or output devices) and communicate (with remote wired or wireless devices)
  • Extra credit: use different languages and/or development environments
  • Extra credit: connect external components to the board

I.Xiao rp2040

The image below shows the different pins on the Xiao rp2040.

The Xiao RP2040 refers to a microcontroller module based on the Raspberry Pi RP2040, which is produced by Seeed Studio. This compact module offers a similar feature set to the RP2040, including dual ARM Cortex-M0+ cores, high-speed I/O interfaces, integrated memory and more. However, it is designed to be smaller and more easily integrated into electronic projects and embedded systems. The “Xiao RP2040” is particularly appreciated by hobbyists and manufacturers for its performance and versatility in a compact format.

Power supply

The Seeeduino XIAO RP2040 can be powered in a variety of ways. Here are some common options:

USB

The XIAO features a USB Type-C connector that can be used to power it. It can be plugged into a computer, USB charger or USB power source to provide the necessary power.

Power pins

The XIAO also features power pins (VCC and GND) on the edge of the module. You can supply power directly to these pins from an external power source, such as a battery or AC adapter. Be sure to observe the voltage specifications recommended by Seeed Studio.

Vin (Voltage In)

If you’re using an external power supply, you can apply it to the Vin pin, which is also available on the edge of the module. Make sure that the voltage supplied is within the range specified by Seeed Studio.

II. Xiao’s rp2040 programming

For more information on this microcontroller, please attach this site. It will give you everything you need to learn how to use your xiao.

Step 1. Launch Arduino IDE software

Step 2. Add the Seeed Studio XIAO RP2040 board package to your Arduino IDE Go to File > Preferences and fill in the additional board manager URLs with the URL below: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

Step 3. Go to Tools > Map > Map Manager… enter the keyword RP2040 in the search field. Select the latest version of Raspberry Pi Pico/RP2040 and install it.

Step 4. Go in Tools > Board > Raspberry PI Pico/ RP2040 > Seeed XIAO RP2040S

III.Codes

III.1 First code

Using python and the Arduino IDE, we’re going to create a graphical interface to turn on and/or off an LED with the circuit we’ve designed 🤗🤗.

🤔🤔 Is that possible you ask!!!

Don’t worry, I’ll show you how!

We’ve put together the Arduino code below 👇👇👇

Code_Test

/**********************************************************/
/************Declaration de variazble**********************/

char SerialData;
int pinLED = D10;


/**********************************************************/
/*     Iniatialisation
/**********************************************************/
void setup() {
  // put your setup code here, to run once:
pinMode(pinLED, OUTPUT);
Serial.begin(9600);
}


/*********************************************************/
/*    Boucle infinie
/*********************************************************/

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available()>0) { //S'il y a des données qui arrivent sur le port série
    SerialData = Serial.read(); //Lecture sur le port

    Serial.print(SerialData); //on affiche les données lue dans la variable "SerialData"
  }

    if(SerialData == '1')
    {
      digitalWrite(pinLED, HIGH); // on allume la LED
    }

    else if (SerialData == '0')
    {
      digitalWrite(pinLED, LOW); // on éteint la LED
    }
}

Open your python editor (we’ll use VS code Studio) and create a (.Py) file.

Enter the following python code in your python editor. Remember, here our board is on the Com9 and we have set a communication speed of 9600 bauds in the Arduino code. Set your own Com and the communication speed you have chosen.

#coding : utf-8

import serial  # importer la biblio python
import tkinter # creation d'interface graphique

arduino_Data = serial.Serial('com9', 9600) # on defini le port où est connecté l'Arduino et 
                                           #la vitesse de communication

def led_on(): #fonction pour allumer la led
    arduino_Data.write(b'1') #b pour byte

def led_off(): #fonction pour allumer la led
    arduino_Data.write(b'0') 

#Création de l'interface graphique

led_controle_windows =tkinter.Tk()
Button =tkinter.Button # on crée un bouton en faisant appel aux fonctions integrees dans tkinter

btn1 = Button(led_controle_windows, text ="ON", command= led_on)

btn2 = Button(led_controle_windows, text ="OFF", command= led_off)

btn1.grid(row=0, column=1) # position bouton1 sur la 1ère ligne 1ère colonne
btn2.grid(row=1, column=1) # position bouton2 sur la 2ème ligne 1ère colonne

led_controle_windows.mainloop

input("presse Entrée pour Quitter")

In the Arduino Editor, upload the code by clicking on the button circled in Red.

In VsCode, click on the button circled in Red and your graphical interface will appear.

This video shows an overview of the project

III.2 Second code

For the second program, we will use an ultrasonic sensor and LED. We are going to write a miropython program under the Thonny software for the xiao rp2040 development board.

To install Seeed Studio Xiao rp2040 with micropython, please refer to the following site : Seeed studio

III.2.1 Programming with MicroPython + Thonny

MicroPython is an implementation of Python 3 optimized to run on microcontrollers and microprocessors with limited resources. It provides a lightweight version of Python that can be run on low-power devices such as microcontrollers, while offering much of the power and flexibility of standard Python syntax. With MicroPython, users can write and run Python code directly on microcontrollers, enabling rapid development of automation, IoT (Internet of Things), sensor and embedded device projects. MicroPython is often used on development boards such as the Raspberry Pi Pico, ESP32, ESP8266, STM32 and others.

Installing

Download Thonny

As I said above, the website Seeed Xiao gives you all the information you need to install micropython.

Functioning

This MicroPython code uses an ultrasonic sensor connected to GPIO pins 26 and 27 of a Seeeduino Xiao to measure the distance to an object. Then, depending on the distance measured, three LEDs connected to pins 13, 14 and 15 are switched on or off.

Code_Test

from machine import Pin, time_pulse_us
import time

# Définir les broches pour le capteur ultrasonique
trig_pin = Pin(26, Pin.OUT)
echo_pin = Pin(27, Pin.IN)

# Définir les broches pour les LEDs
led_pin_20cm = Pin(3, Pin.OUT)
led_pin_40cm = Pin(4, Pin.OUT)
led_pin_60cm = Pin(2, Pin.OUT)

def get_distance():
    # Envoyer une impulsion de déclenchement
    trig_pin.value(1)
    time.sleep_us(10)
    trig_pin.value(0)

    # Mesurer la durée de l'impulsion écho
    duration = time_pulse_us(echo_pin, 1, 30000)  # 30ms timeout

    # Convertir la durée en distance (cm)
    if duration > 0:
        distance = duration / 58  # Conversion en cm (avec un facteur de 58)
    else:
        distance = -1  # Valeur invalide si la mesure a échoué
    return distance

while True:
    distance = get_distance()
    print("Distance:", distance, "cm")

    # Allumer les LEDs à différentes distances
    if distance == -1:
        led_pin_20cm.off() 
        led_pin_40cm.off() 
        led_pin_60cm.off()  
    elif distance <= 20:
        led_pin_20cm.on()  
        led_pin_40cm.off() 
        led_pin_60cm.off()  
    elif distance <= 40:
        led_pin_20cm.on()  
        led_pin_40cm.on()  
        led_pin_60cm.off()  
    elif distance <= 60:
        led_pin_20cm.on()  
        led_pin_40cm.on()  
        led_pin_60cm.on()  
    else:
        led_pin_20cm.off()  
        led_pin_40cm.off()  
        led_pin_60cm.off()  

    time.sleep(1)  # Attendre une seconde avant la prochaine mesure

Project files

File source

File source Arduino

File source python

Links that may help you :


End


Last update: March 31, 2024