Embedded Programming¶
Welcome to week 4 assignment!¶
Key Learning This week was about learning how humans can communicate and interact with technologies! There are three elements required for this revolutionary thing to happen. - The Hardware. - the Software. - The Technology.
1- The Hardware¶
The physical objects we are using in our circuit board. such as: wires, resistors, cheese board, switch, led lights, potentiometer…
p.s.: Potentiometer is a resistor that forms an adjustable voltage divider.
2- The Software¶
In order to communicate with our technology we need to speak their language! therefore we will use a code writing program that is compatible with our boards. In my case I am using Arduino program which is the C language. You can download this program through this Arduino Download there are many sketches ready on the internet I tested 5 sketches shown below. I used this website to understand the commands in my sketch Arduino Reference
The Following is a list of functions that is often used in the Arduino programming language:
-
digitalWrite: Write a high or low value for a digital pin. Syntax: digitalWrite (pin,value)
-
digitalRead: Reads the value from a specified digital pin, either High or Low. Syntax: digitalRead (pin,value)
-
serial.begin: sets the data rate in bits per second. Syntax: serial.begin(speed)
-
serial.print: Prints data to the serial port as human readable text. data can be numbers or text. Syntax: serial.print(value)
-
serial.printIn: Same as the above but in a new line character.
-
delay(): Pauses the program for the amount of time (in milliseconds)
When uploading any sketch, we have to follow certain steps on Arduino software to compile our sketch into the technology board we are using.
These steps are:
-
Tools > Board > Choose board name.
-
Tools> Port > choose port name.
-
Sketch > Verfy/compile.
-
Sketch > upload.
-
if your sketch gives data output go to tools > serial monitor.
-
if your sketch requires identifying a new board that didn’t exist in the drop down menu for boards. go to Files > Preferences > add board manager URL and paste link of boards provided from board data sheet.
Use the following links to add your XIAO microcontrollers boards into your Arduino IDE:
Navigate to File > Preferences, and fill Additional Boards Manager URLs with the url above.
Sketch 1¶
Board: Seeed xiao RP2040¶
this code was provided by my instructor Emma, the Led light turns on and off every 5000milliseconds.
The code:
int led_pin = 3;
void setup() {
// put your setup code here, to run once:
pinMode(led_pin, OUTPUT); //set the led_pin as output
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(led_pin, HIGH); //set the Voltage HIGH (3V) and Led turns on
delay(500); //wait 500millisecond (0.5sec)
digitalWrite(led_pin, LOW); //set the Voltage LOW (ground) and Led turns off
delay(500); //wait 500millisecond (0.5sec)
}
Sketch 2¶
Board: Seeed xiao RP2040¶
This code was provided by my instructor Emma, the led light turns on and off using the switch button.
The code:
int digital_sensor_pin = 27; //change the pin, where the sensor is connected?
int digital_sensor_value = 0; //variable in which we save the sensor voltage
int led_pin = 3; //change the pin of the Led
void setup() {
// put your setup code here, to run once:
pinMode(digital_sensor_pin, INPUT_PULLUP); //initialize the sensor pin
pinMode(led_pin, OUTPUT); //initialize led pin
Serial.begin(9600);
}
void loop() {
digital_sensor_value = digitalRead(digital_sensor_pin); //read the Voltage at pin sensor
//if the sw is pressed, then the led turns on
//otherwise (else) (the sw in not pressed), then the led turns off
//status 1 ==> 5V (switch is not pressed)
//status 0 ==> 0V (switch is pressed)
// check if the pushbutton is pressed.
if(digital_sensor_value == LOW){ //If it is pressed
digitalWrite(led_pin, HIGH); // turn the LED on
Serial.println("the Led is on.");
} else { //If it is NOT pressed
digitalWrite(led_pin, LOW); // turn the LED off by setting the voltage zero
}
}
Sketch 3¶
Board: Seeed xiao RP2040¶
This sketch and code were provided by my instructor Emma.
The code:
int pot_sensor_pin = 27; //change the pin, where the sensor is connected
int pot_sensor_value = 0; //variable in which we save the sensor voltage
void setup() {
// put your setup code here, to run once:
pinMode(pot_sensor_pin, INPUT_PULLUP); //define the pin as INPUT PULLUP
Serial.begin(9600); //open communication
}
void loop() {
// put your main code here, to run repeatedly:
pot_sensor_value= digitalRead(pot_sensor_pin); // read the voltage on the sensor pin
Serial.print("the status of the sensor is: ");
Serial.println(pot_sensor_value); //print the value
delay(100);
}
Using the Potentiometer to write value
Sketch 4¶
Board: Seeed xiao RP2040¶
This sketch and code were provided by my instructor Emma.
Using the Potentiometer to turn on led with a certain value
The code:
int sensorPin = 27; // select the input pin for the potentiometer
int ledPin = 3; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
Sketch 5¶
Board: XIAO ESP32C3¶
This board comes with a dual wifi/bluetooth dual mode chip. it comes with an external antenna to increase signal strength for wireless usage. to start using it we need to add connect board with our computer, navigate to Files > preferences, and fill Additional boards manager URL and past this link : https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json
Navigate to Tools > Board > Boards Manager…, type the keyword “esp32” in the search box, select the latest version of esp32, and install it. Navigate to Tools > Board > ESP32 Arduino and select “XIAO_ESP32C3”
After connecting my board to my laptop and identifying it. copy the following sketch:
The code:
#include "WiFi.h"
void setup()
{
Serial.begin(115200);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup done");
}
void loop()
{
Serial.println("scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
delay(10);
}
}
Serial.println("");
// Wait a bit before scanning again
delay(5000);
}
Go to sketch > compile. sketch > Upload This sketch goal is to provide me with a list of available wifi networks and adding a star * next to unauthaurized networks. the monitor will scan again every 5000milliseconds.
This code was provided by my instructor Emma, for this network I changed the value appearing on the monitor through editing the sketch as follows:
serial.print(i + 2);
Sketch 6¶
Board: XIAO ESP32C3¶
This sketch I found in XIAO_ESP32C3 datasheet.
The code:
#include <WiFi.h>
const char* ssid = "your-ssid";
const char* password = "your-password";
void setup()
{
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{
}
In this sketch I want to connect my board to a WIFI network. Follow all previous steps of: Copying the sketch into Arduino program, Identify Board name, Identify Port, Compile, and Upload.
Sketch 7¶
using the seeed_xiao_RP2040, a push button and a swith. this code will make my light fade.
This code was provided by my instructor Emma, I modefied it to have two led lights fading.
The code:
/* FabAcademy 2023 -Emma Pareschi
In this skect the brithness of the led goes from 0 to the max intensity.
With this sketch you learn about analogWrite and 'for loop'
*/
int led_pin = 3; //define variable for the led pin
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led_pin, OUTPUT); //set the led_pin as output
}
// the loop routine runs over and over again forever:
void loop() {
for (int i = 0; i <= 255; i++) {
analogWrite(led_pin, i);
delay(10);
}
}
below is the modefied code that makes two leds fade.
/* FabAcademy 2023 -Emma Pareschi
In this skect the brithness of the led goes from 0 to the max intensity.
With this sketch you learn about analogWrite and 'for loop'
*/
int led_pin_1 = D3; //define variable for the led pin
int led_pin_2 = D4; //define variable for the led pin
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led_pin_1, OUTPUT); //set the led_pin as output
pinMode(led_pin_2, OUTPUT); //set the led_pin as output
}
// the loop routine runs over and over again forever:
void loop() {
for (int i = 0; i <= 255; i++) {
analogWrite(led_pin_1, i);
analogWrite(led_pin_2, i);
delay(10);
}
}
Group assignment¶
We compared performance speed for different boards using the following sketch:
#include "Arduino.h"
unsigned long timeNow, it = 0;
unsigned long timeLast = 0;
boolean running = true;
void setup() {
Serial.begin(115200);
timeNow = millis() / 1000;
timeLast = timeNow;
}
void loop() {
if (!running)
return;
// Time Range of exact 10 seconds
if (timeLast + 10 > timeNow) {
it = it + 1;
} else {
Serial.print("Duration: ");
Serial.print(timeNow);
Serial.print(" seconds, iterations: ");
Serial.println(it);
running = false;
}
timeNow = millis() / 1000;
}
Board 1 Seed XIAO_ESP32C3 Duration: 10 seconds, iterations: 2634925 Board 2 ESP-wroom-32 Duration:10 seconds, iterations: 1216521
As a conclusion, each microcontroller we tested had features and capabilities, you can choose your microcontroller according to your project’s needs.the RP2040 was the fastest among all microcontrollers therefore I will be working more with this one!
3- The Technology “The board”¶
There are several boards that can be used or made inside of a lab, for this course we are using the seeed Xiao (I’ll explain about each and everyone of them later in this week documentation)
The main reason we chose this technology in particular is their micro-size, their capacity and their cheap price. each Microcontroller has 16 pins, each pin has a number and a function that can be referred to in our sketches.
Seeed Studio XIAO RP2040¶
Xiao RP2040 is compatible with the Raspberry Pi RP2040 ecosystem as they share the same RP2040 chip. It supports multiple languages including C / MicroPython / CircuitPython. This will be a great tool for you to get started with MicroPython.
- Powerful MCU: Dual-core ARM Cortex M0+ processor, flexible clock running up to 133 MHz.
- Rich on-chip resources: 264KB of SRAM, and 2MB of on-board Flash memory
- Flexible compatibility: Support Micropython/Arduino/CircuitPython
- Easy project operation: Breadboard-friendly & SMD design, no components on the back
- Small size: As small as a thumb(20x17.5mm) for wearable devices and small projects.
- Multiple interfaces: 11 digital pins, 4 analog pins, 11 PWM Pins,1 I2C interface, 1 UART interface, 1 SPI interface, 1 SWD Bonding pad interface.
For general I/O pins: Working voltage of MCU is 3.3V. Voltage input connected to general I/O pins may cause chip damage if it’ higher than 3.3V . For power supply pins: The built-in DC-DC converter circuit able to change 5V voltage into 3.3V allows to power the device with a 5V supply via VIN-PIN and 5V-PIN. Please pay attention to use, do not lift the shield cover.
After looking at the basic features, you will find the pinning of the XIAO RP2040. More information in this Link. Here the Seeed Studio XIAO RP2040 pinout sheet.
Seeed Studio XIAO ESP32-C3¶
ESP32-C3 is an IoT mini development board based on the Espressif ESP32-C3 WiFi/Bluetooth dual-mode chip.
- Powerful CPU: ESP32-C3, 32 bit RISC-V single core processor that operates at up to 160 MHz
- Complete WiFi subsystem: Complies with IEEE 802.11b/g/n protocol and supports Station mode, SoftAP mode, SoftAP + Station mode, and promiscuous mode
- Bluetooth LE subsystem: Supports features of Bluetooth 5 and Bluetooth mesh
- Ultra-Low Power: Deep sleep power consumption is about 43μA
- Better RF performance: External RF antenna included
- Battery charging chip: Supports lithium battery charge and discharge management
- Rich on-chip resources: 400KB of SRAM, and 4MB of on-board flash memory
- Rich interfaces: 1xI2C, 1xSPI, 1xI2S, 2xUART, 11xGPIO(PWM), 4xADC, 1xJTAG bonding pad interface
For general I/O pins: This is 5v out from the USB port. You can also use this as a voltage input but you must have some sort of diode (schottky, signal, power) between your external power source and this pin with anode to battery, cathode to 5V pin. Working voltage of MCU is 3.3V.