4. Embedded Programming¶
This week was not easy for me as i absolutely lack concept on this Embedded Programming. However, i am happy that i am getting chance to explore it which is most important to become a part of Fab Lab.
To understand more on Embedded Programming, i need to first understand Programming, Coding and Embedded Programming. I went through this link to to get more about Programming and Coding.
What is Programming?
It refers to the process of giving a set of instructions in the form of codings by programmer to a computer/machine to execute the result.For programming, we have to use codings.
Coding refers to the process of transforming those instructions into a written language that a computer can understand
Embedded Programming
It is the writing software/program which is designed to run embedded systems like microcontroller that perform specific task/function. It requires a deep understanding on both hardware and software to ensure that syetem operates within embedded environment.
Disclaimer: this definition is copied from chatGPT
Assignments for week 4:¶
1. Group Assignment
- Demonstrate and compare the toolchains and development workflows for available embedded architectures.
2. Individual Assignment - Browse through the data sheet for your microcontroller. - Write a program for a microcontroller and simulate its operation, to interact (eith local input &/or output devices) and communicate (with remote wired or wireless connections) extra credit : test it on a development board extra credit:try different langauges &/or development environments.
1. Group Assignment¶
For group assignment for this week, we were asked to explore on the development boards and browse through their datasheets and compare the toolchains and development workflows.
Here is our group assignment link
The development boards available to explore for our group assignments were:
- Arduino UNO
- ESP-WROOM-32
- Xiao RP2040
- Adafruit
From these we have mostly worked on Arduino UNO R3 with ATmega328P microcontroller and XIAO SAMD21 using similar codes. We have used ‘Arduino IDE’ to run the programming.
From this group activities, i have learnt some basics on use of ultrasonic distance sensor, LED and buzzer with resistor, jumper wires and breadboard and thier connections.
Using Arduino UNO board with ATmega328P microcontroller is more easy to use for the beginners like me which provides a large no. of tutorials. I will be using XIAO ESP32 C3 and XIAO SAMD21 microcontrollers for my final project work as it has low power consumptions and requires less memory compared to others.Arduino Uno board does not have Wi-Fi/bluetooth connectivity and hence i will choose to work with XIAO ESP32 C3.
Individual assignment¶
For the individual assignment, i will be using Tinkercad and WOKWI for simulation.
The micorcontrollers available to explore for this assignment were:
- ATmega328P
- XIAO SAMD21
1. Simulation using TinkerCAD using ATmega328P microcontroller of Arduino UNO R3.¶
About Arduino UNO board with ATmega328P MCU¶
Arduino UNO R3 is a popular microcontroller board that uses ATmega328P as its microcontroller. This falls under the Arduino family, which is designed for easy usage in electronics and programming. It is commonly used for building prototypes, learning embedded systems and creating interactive electronic projects.
reference: chatGPT and reference link from here
ii. specifications:
Memory:
- AVR CPU at upto 16MHZ.
- 32 kB Flash
- 2 kB SRAM
- 1 kB EEPROM
Peripherals:
- 2X 8-bit timer/counter.
- 1x16-bit timer/counter
- 1x controller/peripheral Serial Peripheral Interface(SPI)
- 1X DUAL mode controller
- 1X Analog comparator (AC) with a scalable reference input.
- waterdog timer with separate on-chip oscillator.
- Six PWM channels.
- interrupt and Wake-up on pin change.
iii. Pinout:
pinout image source for ATmega328P microcontroller is here
Steps involved for simulation with TinkerCad.¶
I opened the Arduino IDE and selected a fade example code to be used for the Simulation in Tinkercad.Here is the fade example code i used.
/*
Fade
This example shows how to fade an LED on pin 9 using the analogWrite()
function.
The analogWrite() function uses PWM, so if you want to change the pin you're
using, be sure to use another PWM capable pin. On most Arduino, the PWM pins
are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Fade
*/
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
Then, i opened Tinkercad.I choosed ‘Create’ and clicked on ‘Circuits’ and added the components like bread board, LED, 220 ohm resistor and Arduino UNO R3.
After adding the components, gave the connections.
Then choosed the ‘Text’ under the Edit mode and clicked on ‘Continue’ to paste the code.
Then pasted the example code from Arduino IDE and edited the code.
I had edited the code by adding the Void loop function condition where i can monitor the LED fade blink brightness from serial monitor.I can type 1 to fade from brignness 300 to zero, and enter 2 to fade from low to high and waiting to delay by 10 milliseconds.
So my actual code for this simulation was:
/*
Fade with Serial Monitor Control
Send '1' to fade down, '2' to fade up.
*/
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
pinMode(led, OUTPUT); // declare pin 8 to be an output
Serial.begin(9600); // start serial communication
Serial.println("Send '1' to fade down, '2' to fade up.");
}
// the loop routine runs over and over again forever:
void loop() {
if (Serial.available() > 0) {
char command = Serial.read();
if (command == '1') {
Serial.println("Fading Down...");
for (int i = 255; i >= 0; i--) {
analogWrite(led, i);
delay(10);
}
}
else if (command == '2') {
Serial.println("Fading Up...");
for (int i = 0; i <= 255; i++) {
analogWrite(led, i);
delay(10);
}
}
}
}
Then, i clicked on simulation.
Simulation video.¶
Verification of simulation using Arduino IDE.¶
To verify my simulation, i thought of testing using ‘Arduino IDE’. So i downloaded it from this link
To add the board other than arduino, I followed the steps as in the image. I used the URL send by my instructor Tshering Wangzom
I went to File and clicked preferences and pasted the url as shown in the image below.
Then i did the connections using the components required.
Opened the Arduino IDE and selcted a ‘new sketch’ under the ‘File” and delated all as 1 wanted to paste the code from the chatGPT.
- clicked on “Compile/tick” to compile the code followed by clicking on “Upload” to upload the codes to Arduino.
Once done with uploading, go to Tools > Serial monitor or click on ‘Serial Monitor’ at upper right corner. Then, clicked 1 to fade from high to low and 2 to fade from low to high.
Changes to my above code and explanation.¶
** For this code LED is defined by pin 9 which is capable of PWM, variable briggtness defined from 0 and fade brightness from 5.**
Under the void set up function, I wanted my my code to run once by setting LED as OUTPUT that starts serial communication at 9600 baud rate.
Under Loop function, i wanted my code to run continuously when the command is monitored by typing ‘1’ and ‘2’.Typing ‘1’ in serial monitor will let LED fade down from brightness 255 to 0 and typing ‘2’ will enable LED fade up from zero to 255 from dim to light. i set the waiting delay time as 10 milliseconds between the change to take place between briggtness.
2.Simulation using WOKWI for XIAO SAMD21 microcontroller.¶
For this simulation, i used XIAO SAMD21 board which i have fabricated to test LED blinks.And the simulation was done in WOKWI.
My refernce link for XIAO SAMD21 datasheet is here.
About XIAO SAMD21¶
Seeed Studio XIAO SAMD21 has the powerful ATSAMD21G18A-MU and has 14 PINs, which can be used for 11 digital interfaces, 11 mock interfaces, 10 PWM interfaces (d1-d10), 1 DAC output pin D0, 1 SWD pad interface, 1 I2C interface, 1 SPI interface, 1 UART interface, Serial communication indicator (T/R), Blink light (L) through pin multiplexing. Seeed Studio XIAO SAMD21 has a Type-C interface that can supply power and download code and it has two reset contact point.
specifications:
Pin Out for Seeed Studio XIAO SAMD21
For this simulation, i have used XIAO ESP32-C3 as XIAO SAMD21 was not available in WOKI simulation.
I went to the browser and opened WOKWI from this link and choosed “ESP32” and ESP32-C3 example simulation by Urish.
void setup() {
Serial.begin(115200);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
Serial.println("");
Serial.println("Hello, XIAO ESP32-C3!");
Serial.println("Welcome to Wokwi :-)");
}
void loop() {
Serial.println("Red");
digitalWrite(D2, HIGH);
delay(500);
digitalWrite(D2, LOW);
Serial.println("Green");
digitalWrite(D3, HIGH);
delay(500);
digitalWrite(D3, LOW);
Serial.println("Blue");
digitalWrite(D4, HIGH);
delay(500);
digitalWrite(D4, LOW);
}
i had added 2 LEDs. red LED to pin D2 and and 330 ohm resistors with XIOA ESP32 C3 MCU.And my connection as below.Here is my final code.
void setup() {
Serial.begin(115200);
pinMode(D2, OUTPUT);
pinMode(D10, OUTPUT);
Serial.println("");
Serial.println("Hello,JNEC FABLAB");
Serial.println("Welcome to World of Innovation :-)");
}
void loop() {
Serial.println("Red");
digitalWrite(D2, HIGH);
delay(1000);
digitalWrite(D2, LOW);
Serial.println("Green");
digitalWrite(D10, HIGH);
delay(1000);
digitalWrite(D10, LOW);
}
My edited code above explains that for the setup function, the serial communication starts from 115200 baud rate where the pin D2 and pin D10 of XIAO SAMD21 are OUTPUT.
I wanted my code to print greetings ‘Hello, JNEC FABLAB’ followed by ‘Welcome to the world of Innovation’ when the code is uploaded and clicked on serial monitor.
Under the void loop function, it will allow the red LED to blink using pin D2 high that lasting for a second.After 1 second, using pin D10 it will allow green LED to blink for a second and the serial communication continues making each LED blink for 1 second.
Here is the simulation in WOKWI.
I wanted to try connecting the above simulation using the my board made during Electronics design week and Electronics Production week.
I had used my fabricated board of XIAO SAMD21, jumper wires, 2 LEDS, two 330 ohm resistors and bread board. Here is my connections where i changed the one of the LED pin as D10.
I opened the Arduino IDE and selected new sketch and pasted the code.
Next, i had connected my board and its connection by USB to the computer.Then selected board and port.
To run the program, i clicked on compile and upload.After that i clicked on serial monitor and first LED blinked for a second and other LED blink too for a second.The LEDs blinks continuously changing to actuate each after a second.
Learning outcomes:¶
This week is never easy for me as i have to study on the datasheets of different microcontrollers available in my lab. I thought that my works will get more slower compared to earlier weeks. I still have to learn more about the microcontrollers.
Shortly, for this week, atleast i could get basic ideas on the components required for basic project using programming. I have learnt to connect LED and ultrasonic distance sensor with XIA0 ESP32 C3 and ATmega328P chips.And i got basic explosure on simulating using these chips on Tinkercad and WOKWI.
For my Final project programming activities, i am planning to use XIAO SAMD21 and XIAO ESP32 C3 with components like Mini water pump and ultrasonic distance sensor for Automatic saop Sanitizer Dispenser as my project need Wi-Fi/bluetooth connections.
Disclaimer: for simulation 1 using TinkerCad, i used Fade example code from Arduino IDE and edited it and explained my understanding.For simulation with WOKWI i tried to edit some and have wrote my understandings.