WEEK 6 - EMBEDDED PROGRAMMING

THIS WEEK, OUR OBJECTIVE IS TO LEARN ABOUT MICROCONTROLLERS AND HOW TO PROGRAM THEM.i will be using the #Quentorres i made in the WEEK 4 - ELECTRONICS PRODUCTION.

assignment

Individual assignment:

  • write a program for a microcontroller development board that you made,to interact (with local input &/or output devices) and communicate (with remote wired or wireless devices)
  • extra credit: use different languages &/or development environments
  • extra credit: connect external components to the board
    group assignment:
  • Browse through the data sheet for your microcontroller
  • compare the performance and development workflows for other architectures

group assignmnet

You can find our group page here and this week group ASSIGNMENT here..this week browsed through many datsheets

COmparison.jpg
comparison chart of different microcontroller boards -group assignment

about Quentorres

history of  fabboards.jpg
HISTORY OF boards in fab academy

Pasted image 20240229142942.jpg

circuit diagram of #Quentorres

Pasted image 20240229120645.jpg

OUR BOARD #XIAORP2040 IS COMPATIBLE WITH THE ARDUINO LIBRARY.

THIS BOARD EMBED WIRE RP2040 DEVELOPED BY RASPBERRY .

Pasted image 20240301193026.jpg

programming the xiao rp2040 with arduino ide

alogirthm for blinking

step 1. start

step 2. initialise the pin

step 3.turn on the led

step4. add a delay

step5. turnoff and go to step 3

// the setup function runs once when you press reset or power the board

void setup() {

// initialize digital pin LED_BUILTIN as an output.

pinMode(0, OUTPUT);

}

  
// the loop function runs over and over again forever

void loop() {

digitalWrite(0, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

digitalWrite(0, LOW); // turn the LED off by making the voltage LOW

delay(100); // wait for a second

}

you can assign a variable for led pin and change from it.

int ledPin = 0;

// the setup function runs once when you press reset or power the board

void setup() {

  

pinMode(ledPin, OUTPUT);

}

  

// the loop function runs over and over again forever

void loop() {

digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

digitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOW

delay(100); // wait for a second

}



select and change values of pin by looking into xiao pinout














button and led

you can use the default code in examples which works fine or can choose to write your own
Screenshot 2024-02-29 at 12.07.48 PM.jpg
default example code for digital button in arduino

algorithm fro simple button and switch

step 1. start

step 2. initialise button and led pins

step 3. setup button pin as input and ledpin as output

step 4. if button is pressed, then led should turn on, go to step 6

step 5. turn led off

step 6. go to step 4


const int buttonPin = 27; // the number of the pushbutton pin

const int ledPin = d7; // the number of the LED pin

  


int buttonState = 0; // variable for reading the pushbutton status

  

void setup() {

// initialize the LED pin as an output:


pinMode(ledPin, OUTPUT);


// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

}

  

void loop() {

// read the state of the pushbutton value:

buttonState = digitalRead(buttonPin);

  

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:

if (buttonState == HIGH) {

// turn LED on:

digitalWrite(ledPin, HIGH);

} else {

// turn LED off:

digitalWrite(ledPin, LOW);

}

}

the result is simple and direct

using boolean function to use button as switch

Boolean means a result that can only have one of two possible values: true or false.

here we nee d the push button to act as a switch .

algorithm for button and led with boolean flag

step1. start

step2. initialise the pin numbers

step3. setup the led pin as output and button pin input

step4. if the button is pressed set a flag to invert.

step5. turn ledpin to the state of the flag

step5. go to step 4



const int buttonPin = 27; // the number of the pushbutton pin

const int ledPin = D7;

// the number of the LED pin

bool flag = false;

  

// variables will change:

int buttonState = 0; // variable for reading the pushbutton status

  

void setup() {

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

}

  

void loop() {

// read the state of the pushbutton value:

buttonState = digitalRead(buttonPin);

  

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:

if (buttonState == HIGH)

{

if(flag = !flag); //flag=!flag

  

}

digitalWrite (ledPin, flag);

}

the result seems like having some issue.


the button is working sometimes and not wrking otherstimes

THIS IS DUE TO THE LIMITATIONS OF THE PHYSICAL BUTTON COMING in BETWEEN . SO, WE NEED TO INTRODUCE A DELAY TO ACCOUNT FOR "BOUNCING". THIS ISSUE IS ADDRESSED IN THE FOLLOWING VIDEO.

so i rewrited this delay(200) code by adding a delay in the loop to introduce "debouncing".



const int buttonPin = 27; // the number of the pushbutton pin

const int ledPin = 1;

// the number of the LED pin

bool flag = false;

  

// variables will change:

int buttonState = 0; // variable for reading the pushbutton status

  

void setup() {

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

}

  

void loop() {

// read the state of the pushbutton value:

buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:

if (buttonState == HIGH)

{

delay(200);// delay given to debounce the button.

if(flag = !flag); //flag=!flag

  

}

digitalWrite (ledPin, flag);

now the issue is resolved.

aNOTHER ISSUE AROSE WHERE THE LED BEGINS BLINKING CONTINUOUSLY WHEN PRESSED.

i introduced one more BOOLEAN check to RESOLVE this issue.



const int buttonPin = 27; // the number of the pushbutton pin

const int ledPin = 1;

// the number of the LED pin

bool flag = false;

bool pressed = false;

  

// variables will change:

int buttonState = 0; // variable for reading the pushbutton status

  

void setup() {

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

}

  

void loop() {

// read the state of the pushbutton value:

buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:

if (buttonState == HIGH)

{

if (pressed == false) // introduced thin boolean variable to cater teh blinking effect

{

flag = !flag; //flag=!flag

pressed = true;

}

  
  
  

}

else{

pressed = false;

delay(50);

}

  
  

  
  

digitalWrite (ledPin, flag);

  
  
  
  
  

}

i think now all the above mentioned issues are resolved.

using the serial monitor


const int buttonPin = 27; // the number of the pushbutton pin

const int ledPin = 1;

// the number of the LED pin

bool flag = false;

bool pressed = false;

  

// variables will change:

int buttonState = 0; // variable for reading the pushbutton status

  

void setup() {

Serial.begin(9600);//initalise serial on 9600 baud rate

  

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

  

delay(1000);

Serial.println("led test");

  

}

  

void loop() {

// read the state of the pushbutton value:

buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:

if (buttonState == HIGH)

{

if (pressed == false) // introduce this boolean variable to cater teh blinking effect

{

Serial.println(buttonState);

Serial.println("button is pressed");

flag = !flag; //flag=!flag

pressed = true;

}

  
  
  

}

else{

pressed = false;

delay(50);

}

  
  

  
  

digitalWrite (ledPin, flag);

  
  
  
  

}

Screenshot 2024-02-29 at 6.03.42 PM.jpg

recursive function

lets explore recursive function with the help of llm - chat gpt3.5 turbo for time being..

prompt

write an Arduino sketch that toggles through a series of LEDs when a push button is pressed? The LEDs should be connected to pins 26, 0, and 1, and the push button should be connected to pin 27. Please include code for debouncing the button

prompt
write an Arduino sketch that toggles through a series of LEDs when a push button is pressed? The LEDs should be connected to pins 26, 0, and 1, and the push button should be connected to pin 27. Please include code for debouncing the button

the promt i used for chat gpt

// constants won't change. They're used here to set pin numbers:

const int buttonPin = 27; // the number of the pushbutton pin

const int ledPins[] = {26, 0, 1}; // array of LED pins

const int numLeds = sizeof(ledPins) / sizeof(ledPins[0]); // number of LEDs

int currentLedIndex = 0;

  

// variables will change:

int buttonState = 0; // variable for reading the pushbutton status

  

void setup() {

Serial.begin(9600); // initialize serial on 9600 baud rate

  

// initialize the LED pins as outputs:

for (int i = 0; i < numLeds; i++) {

pinMode(ledPins[i], OUTPUT);

}

  

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

  

delay(1000);

Serial.println("led test");

}

  

void toggleLED(int index) {

// Turn off all LEDs

for (int i = 0; i < numLeds; i++) {

digitalWrite(ledPins[i], LOW);

}

// Turn on the specified LED

digitalWrite(ledPins[index], HIGH);

}

  

void loop() {

// read the state of the pushbutton value:

buttonState = digitalRead(buttonPin);

  

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:

if (buttonState == HIGH) {

// Toggle to the next LED

currentLedIndex = (currentLedIndex + 1) % numLeds;

// Turn on the LED at the current index

toggleLED(currentLedIndex);

// Wait for button release

while (digitalRead(buttonPin) == HIGH) {

delay(50); // debounce delay

}

}

}

code chat gpt provided

the result

programming the xiao rp2040 with micropython (mac)

micropython is a software implementation of a programming language largely compatible with Python 3, written in C, that is OPTIMISED to run on a microcontroller. i am using thonny - a python ide to code micropython.

SEEED STUDIO HAS DOCUMENTATION FOR SETTING UP MICROPYTHON ON THE XIAO RP2040. I'M FOLLOWING IT TO SET IT Up

i downlaoded the the thonny editor from this link - https://thonny.org/ or you can use this command on terminal brew install thonny

Screenshot 2024-03-01 at 11.20.57 AM.jpg

got to options in tool bar.
Screenshot 2024-03-01 at 12.25.29 PM.jpg

select micropython(pico) as the interepter option to run the code.

Press and hold the "BOOT" button and then connect the Seeed Studio XIAO RP2040 to the PC through the Type-C cable. If it works well, there is an "RPI-RP2" desk shown on the mac

and for mac i would recommend to select the port manually.

Screenshot 2024-03-01 at 12.27.48 PM.jpg
and then click the "install and update MICROPYTHON option".

Screenshot 2024-03-01 at 11.30.58 AM.jpgon the upcoming window select the target volume.

i filled the remaining columns:

VARIANT:RASPBERRY pi. pico/pico h
VERSION: choose a stable version

Screenshot 2024-03-01 at 11.32.14 AM.jpg
and click install.you can see it DOWNLOADING. and close it after 'done'

Screenshot 2024-03-01 at 12.46.47 PM.jpg
if SUCCESSFUL you can see the this on shell

by following the DOCUMENTATION i copied the below code and apsted it into the thonny.

from machine import Pin, Timer
 
led = Pin(25, Pin.OUT)
Counter = 0
Fun_Num = 0
 
def fun(tim):
    global Counter
    Counter = Counter + 1
    print(Counter)
    led.value(Counter%2)
 
tim = Timer(-1)
tim.init(period=1000, mode=Timer.PERIODIC, callback=fun)

click run current script

Screenshot 2024-03-01 at 11.34.16 AM.jpg
a window will appear for frist time asking where to save the file.you can choose pico/computer.the code was working for me .

then i CHANGED the pin number to 26 to get this output

from machine import Pin, Timer
 
led = Pin(26, Pin.OUT)
Counter = 0
Fun_Num = 0
 
def fun(tim):
    global Counter
    Counter = Counter + 1
    print(Counter)
    led.value(Counter%2)
 
tim = Timer(-1)
tim.init(period=1000, mode=Timer.PERIODIC, callback=fun)


the result blinliking pin 26 on #Quentorres

then i i DOWNLOADED a library named ws2812.py to blink the neopixel on the #XIAORP2040

copied it into the THONY IDE. and i tried to save the librabry into #XIAORP2040 but it showed error.

Screenshot 2024-03-01 at 11.37.21 AM.jpg

but i booted again and treid it worked.

run code to to light the neopixel and it worked.

from ws2812 import WS2812
import utime
import machine
power = machine.Pin(11,machine.Pin.OUT)
power.value(1)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
WHITE = (255, 255, 255)
COLORS = (BLACK, RED, YELLOW, GREEN, CYAN, BLUE, PURPLE, WHITE)

led = WS2812(12,1)#WS2812(pin_num,led_count)

while True:
    print("Beautiful color")
    for color in COLORS: 
        led.pixels_fill(color)
        led.pixels_show()
        utime.sleep(0.2)

the result

ALMOST ANYTHING AN ARDUINO CAN DO CAN ALSO BE ACHIEVED WITH A MICROPYTHON BOARD. HOWEVER, ONE IMPORTANT THING TO NOTE IS THAT MICROPYTHON CODE ISN'T AS FAST AS ARDUINO CODE.

connecting ultrasound sensor tp #XIAORP2040

Pasted image 20240301192448.jpg

FROM DATATSHEET

you need to boot and reset #XIAORP2040 when coming back to arduino after BOOT LOADING micropython environment.

hcsr04 library is already installed in my arduino ide. so laoded a sample code and tested.

#include <HCSR04.h>

  

UltraSonicDistanceSensor distanceSensor(4, 2); // Initialize sensor that uses digital pins 13 and 12.

  

void setup () {

Serial.begin(9600); // We initialize serial connection so that we could print values from sensor.

}

  

void loop () {

// Every 500 miliseconds, do a measurement using the sensor and print the distance in centimeters.

Serial.println(distanceSensor.measureDistanceCm());

delay(500);

}

Pasted image 20240624192801.jpg
circuit diagram

reflection

this week i had basically bunch of embedded programming doing different experiments. its a nice weel