Week 4. Embedded programming¶
For me embedded programming is completely a new topic, it is like driving a truck after learning how to ride bycycle and on top of that I am a quite slow learner, so I faced difficulty in understanding its main fundamental. Even though it was a challenging task for me to understand and move forward, I had explored tutorials and also didn’t hesitate to get help from my instructors and colleagues.
We were given bread board, micro controller, jumper wires, LED and resistor and were told to do simple connection to make LED ON and OFF. At first I was stucked not knowing what to do and how to approach further but after sometime with appropriate guidance from our instructor I was able to make LED ON and OFF which indeed boosted me to learn and explore more on the topic.
What is program?¶
A program is a set of instructions written in a programming language that tells a computer or microcontroller what to do. It can perform calculations, control hardware, process data, or automate tasks.
Disclaimer: The definition has been copied from ChatGPT
What is programming?¶
Programming is the process of writing instructions (code) that a computer, microcontroller, or other computing device can execute to perform a specific task. These instructions are written in a programming language (like C, Python, or Java) and are then compiled or interpreted into machine code that the hardware understands.
Disclaimer: The definition has been copied from ChatGPT
Assignment of the week¶
Group Assignment
- Demonstrate and compare the toolchains and development workflows for available embedded architectures(microcontrollers)
Individual Assignment
- Explore on the microcontrollers which are available in the lab.
- Browse through the datasheet for your microcontroller and reflect it in the documentation.
- write a program for a microcontroller,and simulate its operation, to interact (with local input or output devices) and communicate (with remote wired or wireless connections)
List of simulator that I will use for simulation:
- Adafruit Circuit Playground
- Tinkercad
- Wokwi
Link to the group assignment is here
1.Adafruit (SAM D21)¶
Disclaimer: The image has been downloaded from google for reference purpose
The SAM D21/DA1 is a series of low-power microcontrollers based on the 32-bit Arm® Cortex®-M0+ processor. They feature up to 256 KB Flash, 32 KB SRAM, and operate at 48 MHz with a performance of 2.46 CoreMark/MHz. The series offers various configurations from 32 to 64 pins, with identical peripheral modules and migration paths. Key features include a 12-channel Direct Memory Access Controller, Event System, capacitive touch support, in-system programmable Flash, USB 2.0 interface, up to six Serial Communication Modules, 12-bit ADC, 10-bit DAC, and various timers. These microcontrollers are designed for energy efficiency with sleep modes and optimized clocking for low power. Development tools include C compilers, debuggers, and evaluation kits.
Disclaimer: The description has been abstracted from the datasheet and summarised using ChatGPT
Configuration Summary
-
Program in memory(kb)- 256 Kb
-
Data memory(Kb)- 32 Kb
-
No of pins- 48
-
Packages- TQFP, QFN
-
USB- Yes
-
Sercom- 6
-
TCC- 3
-
I2S- Yes
-
RTC- Yes
-
WDT- Yes
-
Digital Input/ Output pins- 11
-
Analog Input/ output pins- 11
-
DAC- yes
-
PTC- 120/10
-
Operating voltage- 3.3v
-
Processor- Arm Cortex -MO+
-
Power Consumption- 10.5 µA to 11.3 mA
-
On board LEDs- power LED and Status LED
I tried experimenting on the SAMD21 board by connecting it to my laptop through USB cable and opened Adafruit circuit playground
After that I just gave the program file name and saved my program.
Next I opened the saved file from my local disk and copied it.
Next I opened the CPLAYBOOT(E) and pasted the program file which I had copied and then program start executing.
Adafruit circuit playground allows to work with both blocks program and text program which is javascript. As I was more comfortable working with blocks program, so I chose blocks program.
Since to produce output signal in other microcontrollers, we need to connect buzzer separately, but in adafruit we are having in built small piezo buzzer(also known as piezo speaker). This piezo buzzer is a electronic component that vibrates when electric signal is applied, producing sound in different tones.
Video below shows writing a program and simulating
2. Arduino¶
The Arduino Uno is one of the most popular boards in the Arduino family and is widely used for prototyping and learning electronics and programming. The microcontroller chip present in the Arduino Uno is the ATmega328P. This chip is manufactured by Atmel (now a part of Microchip Technology).
Disclaimer: The definition has been directly copied from ChatGPT
Configuration Summary
• Architecture: 8-bit AVR RISC
• Clock Speed: 16 MHz
• Flash Memory: 32 KB (0.5 KB used by the bootloader)
• SRAM: 2 KB
• EEPROM: 1 KB
• Digital I/O Pins: 14 (6 of which can provide PWM output)
• Analog Input Pins: 6
• Communication Interfaces: UART, I2C, and SPI
Exploring Arduino IDE¶
The Arduino IDE is an open-source software, which is used to write and upload code to the Arduino boards. The IDE application is suitable for different operating systems such as Windows, Mac OS X, and Linux. It supports the programming languages C and C++. Here, IDE stands for Integrated Development Environment.
*Disclaimer: The definition has been copied from google for reference purpose
As there are lot of programing languages and also all of them being completely new to me, I didn’t know which to study but since Arduino IDE supports C and C++, so I decided to use C++ first and do simple programing.
Steps to follow while downloading Arduino IDE
I downloaded Arduino IDE
I opened the Arduino IDE, clicked on File and hovered down to preference option
I added the board core definition on the preference option and clicked ok
Then I clicked on the tools on the drop down menu to add the board from the board manager and selected Arduino UNO since I am going to work with Arduino
Hands on working with bread board, Arduino, LED, jumper wires and resistor
Before doing connection on the bread board I just calculated the resistor’s value assuring that it wont burn out LED while doing connection so for that i just calculated using Ohm’s law as;
After that I started laying out the equipments on the table and started to connect them.
After I finished connecting all the circuits together, I gave the power supply to the Arduino board by connecting it to my laptop via USB cable and found that my circuit is complete because LED blinked.
So next what I thought was to make this LED blink in continous loop and for that I just referred this tutorial and followed his code for programing. After going through this tutorial I was able to make LED blink continuously and I really enjoyed doing that.
Initially I tried connecting the postive terminal of LED to pin NO 7 of Arduino board and programmed on Pin NO 7
Attached below is a code that I used for programing
int led1=7;
void setup()
{
//pinMode(pin,mode);
pinMode(7,OUTPUT);
}
void loop()
{
digitalWrite(led1,HIGH);
delay(1000);
digitalWrite(led1,LOW);
delay(1000);
}
After programing I just saved my code
Selected the location where I wanted to save the file
As I had already selected board as Arduino, to upload the file I need to select the port so I selected the COM7(Arduino Uno)
After that click on the compile button which is indicated by TICK sign and on upload button which is indicated by Arrow sign below the drop down menus.
Finally after compiling and uploading the program my LED started blinking
After that I tried simulating the fade test using Arduino IDE and for that also I referred same tutorial and tried copying a code from chatGpt, compiled and uploaded using Arduino IDE.
Attached below is a code that I used for programing
const int ledPin = 10;
int brightness = 0;
int fadeAmount = 15;
int delayTime = 150;
void setup()
{
//pinMode(pin,Mode);
pinMode(10,OUTPUT);
}
void loop()
{
// Fade in
for (brightness = 0; brightness <= 255; brightness += fadeAmount) {
analogWrite(ledPin, brightness);
delay(delayTime);
}
// Fade out
for (brightness = 255; brightness >= 0; brightness -= fadeAmount) {
analogWrite(ledPin, brightness);
delay(delayTime);
}
}
Disclaimer: The above code has been copied from chatGpt
Next I connected ultrasonic sensor with Arduino board and tried to run the program and do simulation using both development board and Tinkercad.
I just clicked on the code and copied the code.
Followed by that I opened Arduino IDE, compiled and uploaded the code.
After that I opened the tinkercad and tried simulating there by connecting the components together in which I connected Trig pin to pin No 10 and Echo pin to Pin No 9
Simulation Video using Tinkercad
3. ESP32C3¶
The XIAO ESP32C3 is a compact IoT development board based on the Espressif ESP32-C3 chip, featuring a 32-bit RISC-V CPU with an FPU for powerful computing. It supports Wi-Fi (IEEE 802.11 b/g/n) and Bluetooth 5 (BLE), with an external antenna for enhanced signal strength. The board has a small, surface-mountable design, 11 digital I/O (PWM), 3 analog I/O (ADC), and supports UART, I2C, and SPI interfaces. It includes reset and bootloader buttons and is compatible with Grove Shield for Seeeduino XIAO (except SWD contacts). Positioned as a high-performance, low-power, cost-effective solution, it is ideal for IoT and wearable applications
Disclaimer: The definition has been copied from chatGpt for reference purpose
Configuration Summary
-
Powerful CPU: ESP32-C3, 32bit RISC-V singlecore 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
-
Ultra small size: As small as a thumb(21x17.8mm) XIAO series classic form-factor for wearable devices and small projects
-
Reliable security features: Cryptographic hardware accelerators that support AES-128/256, Hash, RSA, HMAC, digital signature and secure boot
-
Rich interfaces: 1xI2C, 1xSPI, 2xUART, 11xGPIO(PWM), 4xADC, 1xJTAG bonding pad interface
-
Single-sided components, surface mounting design
Using Wokwi as a simulator
Wokwi is an online Arduino simulator that allows you to design, prototype, and simulate Arduino circuits without needing physical hardware. It supports various microcontrollers like Arduino Uno, ESP32, ATtiny85, and even Raspberry Pi Pico.
Disclaimer: The definition has beed copied from ChatGpt
After working with tinkercad, I shifted towards Wokwi for designing the online circuits with different components. I decided to use both Arduino and ESP32C3
Work flow for the wokwi using Arduino
Since Wokwi being the online platform just type its name in the google and open the simulator.
Select the microcontroller that you are going to work with from the list.
As a part of my final project, I thought of designing a circuit board consisting of Lcd, servo motor, resistors, ultrasonic distance sensor and leds with Arduino Uno using Wokwi as a simulator and for that also I decided to use C++ as a programing language.
Since I am adding few components new here which are Lcd(16*2), servo motor and potentiometer. As they being the new components I referred to their datasheets for the connection which are as follows:
LCD
Servo motor
Potentiometer
After referring to the datasheets of components mentioned above I was able to connect the circuits together and below I explained the connection details of different pins.
Explaining the connection¶
Ultrasonic distance sensor
-
Connect the TRIG PIN to PIN 3 (Arduino)
-
Connected the ECHO PIN to PIN 4 (Arduino)
-
Connect the VCC to VCC (Arduino)
-
Connect Ground to Ground (Arduino)
LCD (16*2)
-
Connect PIN 1(VSS) to Ground (Arduino)
-
Connect PIN 2 to VCC (Arduino)
-
Connect PIN 3 to middle pin of potentiometer
-
Connect PIN 4(RS) to PIN 7 (Arduino)
-
Connect PIN 5(RW) to GROUND (Arduino)
-
Connect PIN 6(E) to PIN 8 (Arduino)
-
Connect PIN 11(D4) to ~ 9 (Arduino)
-
Connect PIN 12(D5) to ~ 10 (Arduino)
-
Connect PIN 14 to PIN 12 (Arduino)
Potentiometer
-
Connect VCC to VCC (Arduino)
-
Connect GROUND to GROUND (Arduino)
-
Connect middle pin to PIN 3 (LCD)
Servo motor
-
Connect the GROUND to GROUND (Arduino)
-
Connect the VCC to VCC (Arduino)
-
Connect(PIN 3) to ~ 5 (Arduino)
LED Red - Connect VCC(LED) to PIN 13(Arduino) through a resistor
- Connect GROUND to GROUND (Arduino)
LED Green
-
Connect VCC(LED) to ~ 6 through a resistor
-
Connect GROUND to GROUND
This is how my circuit diagram looked like after doing connection
For the program to run I just got help from ChatGpt, copied the code from there and added in wokwi for the simulation and it worked.
Attached below is a code that I used for programing
#include <LiquidCrystal.h>
#include <Servo.h>
#define TRIG_PIN 3
#define ECHO_PIN 4
#define RED_LED 13
#define GREEN_LED 6
#define SERVO_PIN 5
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
Servo myServo;
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
myServo.attach(SERVO_PIN);
lcd.begin(16, 2);
}
long getDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2; // Convert to cm
}
void loop() {
long distance = getDistance();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm");
if (distance < 10) {
digitalWrite(RED_LED, HIGH);
digitalWrite(GREEN_LED, LOW);
myServo.write(90);
lcd.setCursor(0, 1);
lcd.print("Object Detected!");
} else {
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
myServo.write(0);
lcd.setCursor(0, 1);
lcd.print("No Object.");
}
delay(500);
}
Simulation video using Wokwi simulator
Using ESP32C3 and simulating in WOKWI¶
Click on the New project and select the micro controller from the list. This time I selected language as a MICROPYTHON for coding.
I tried to design a simple circuit comprising of red led and yellow led to make them blink alternatively with certain delay.
For that I connected the positive terminal of red led to PIN 2 and yellow led to PIN 4 and ground to ground connection
Attached below is a code that I used for programing
from machine import Pin
import time
# Define LED pins
red_led = Pin(2, Pin.OUT) # Red LED on GPIO 2
yellow_led = Pin(4, Pin.OUT) # Yellow LED on GPIO 4
while True:
red_led.value(1) # Turn ON Red LED
yellow_led.value(0) # Turn OFF Yellow LED
time.sleep(1) # Wait 1 second
red_led.value(0) # Turn OFF Red LED
yellow_led.value(1) # Turn ON Yellow LED
time.sleep(1) # Wait 1 second
Disclaimer: The programing code has been copied from ChatGPT for reference purpose
Simulation video using Wokwi simulator
4. Xiao RP2040¶
The Xiao RP2040 is a compact microcontroller board developed by Seeed Studio, featuring the Raspberry Pi RP2040 microcontroller chip. It is designed for a wide range of embedded projects, offering a balance of performance, size, and power efficiency. Below are some key features and details about the Xiao RP2040:
Disclaimer: The definition has been copied from ChatGpt
Configuration Summary
-
Microcontroller:
-
RP2040 (Dual-core ARM Cortex-M0+ processor, up to 133 MHz).
-
264 KB of SRAM.
-
2 MB of onboard flash memory.
-
Form Factor:
-
Ultra-compact design (21 x 17.5 mm), making it suitable for space-constrained projects.
-
GPIO Pins:
-
14 GPIO pins (supports digital I/O, PWM, I2C, SPI, UART, and analog input).
-
11 of these pins support 12-bit ADC (Analog-to-Digital Conversion).
-
Power:
-
Operating voltage: 3.3V.
-
USB Type-C for power and programming.
-
Supports battery-powered applications.
-
Connectivity:
-
USB 1.1 (for programming and communication).
-
I2C, SPI, and UART interfaces for external peripherals.
-
Programming:
-
Programmable via C/C++ using the Raspberry Pi Pico SDK or MicroPython.
-
Compatible with the Arduino IDE (with appropriate board support).
-
Additional Features:
-
Built-in RGB LED for status indication.
-
Reset button and boot button for easy programming and debugging
Simulating using Wokwi with Raspberry pi pico
Since I couldn’t find the RP2040 in Wokwi simulator, I used Raspberry pi pico for the simulation and I used Micropython as a language.
After that I made a simple circuit consisting of leds, resistors and connected it to raspberry pi pico.
After that I copied Micropython code from chatGpt and added in Wokwi for the simulation.
Attached below is a code that I used for programing
from machine import Pin
import utime
# Define LEDs
red_led = Pin(10, Pin.OUT) # Red LED on GP10
blue_led = Pin(11, Pin.OUT) # Blue LED on GP11
while True:
red_led.value(1) # Red LED ON
blue_led.value(0) # Blue LED OFF
utime.sleep(0.5) # Wait 0.5 sec
red_led.value(0) # Red LED OFF
blue_led.value(1) # Blue LED ON
utime.sleep(0.5) # Wait 0.5 sec
Simulation video using Wokwi simulator
Exploring on Thonny¶
Thonny is a beginner-friendly Python IDE (Integrated Development Environment) designed for ease of use, especially for those learning programming.
Key Features:
-
Simple Interface – Minimal distractions, ideal for beginners.
-
Built-in Python Interpreter – Comes with Python pre-installed.
-
Debugging Tools – Step-through execution to see how code runs.
-
MicroPython Support – Works with microcontrollers like ESP32, ESP8266, and RP2040 (Raspberry Pi Pico, XIAO RP2040, etc.).
-
Package Manager – Easily install and manage Python libraries.
-
Variable Explorer – View and edit variable values during execution.
Disclaimer: The description mentioned above was obtained from ChatGpt for the reference purpose
Steps to follow while downloading Thonny
Download the Thonny
Open Thonny and go to Tools on Menu bar and hover down to Options.
After you click on options, a new dialog box will appear which will be labelled as Thonny options which allows you to select the list of interpreter and port you are going to work with and download its content.
Connect the ESP32 board with laptop using USB cable keeping the Thonny open and press on the boot button.
For the test I just typed simple script and it was printed on the shell of Thonny.