Skip to content

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:

  1. Arduino UNO
  2. ESP-WROOM-32
  3. Xiao RP2040
  4. Adafruit

From these we have mostly worked on Arduino UNO R3 with ATmega328P microcontroller and ESP-WROOM-32 boards 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 this microcontroller 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 it is not needed for my final project.

I came to know that ESP32 has high processor of 32-bit and has high memory which can be used for complex projects.ESP32 has Wi-Fi/bluetooth connectivity and has more output pins compareed to Arduino.

Individual assignment

For the individual assignment, i will be using Tinkercad and WOKWI for simulation.

The micorcontrollers available to explore for our group assignments were:

  1. ATmega328P
  2. ESP-32S
  3. RP2040

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

atmega328p

1. Simulation using TinkerCAD using ATmega328P microcontroller of Arduino UNO R3.

Steps involved for this work:

i. Wiring the components

I have opened Tinkercad and search for the components required for my work and drag them.And have given connections as followed:

wiring components

  1. ultrasonic sensor-

    • ‘ucc’ to 5v on Arduino board (red jumper wire)
    • ‘Gnd’ to GND on Arduino board(purple jumper wire)
    • ‘trig’ to digital pin 9(yellow jumper wire)
    • ‘Echo’ to digital pin 10(blue jumper wire)
  2. LED - anode to digital pin 13 and cathode to GND through 220 ohm resistor.

wiring components connection

ii. Writing the Arduino Code.

I have refered the idea and paste the code from the chatGPT.I went to ‘code” and selected “text”

process adding code

Then, clicked on “continue” and delated the existing code template and pasted my copied codes brought from chatGPT.

code pasted

codes below:

const int trigPin = 9;  // Trigger pin of the ultrasonic sensor
const int echoPin = 10; // Echo pin of the ultrasonic sensor
const int ledPin = 13;  // LED pin

long duration;  // Variable to store the duration of the pulse
int distance;   // Variable to store the calculated distance

void setup() {
  // Start serial communication for debugging
  Serial.begin(9600);

  // Set up the ultrasonic sensor pins
  pinMode(trigPin, OUTPUT); // Set trig pin as an output
  pinMode(echoPin, INPUT);  // Set echo pin as an input

  // Set up the LED pin
  pinMode(ledPin, OUTPUT);  // Set LED pin as an output
}

void loop() {
  // Clear the trigPin by setting it LOW:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);  // Wait for 2 ms to ensure clean signal

  // Set the trigPin HIGH for 10 microseconds to send the pulse
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the duration of the echo pulse
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance in cm (Speed of sound: 343 m/s)
  distance = (duration / 2) * 0.0343;

  // Print the distance to the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Check if the distance is less than or equal to 20 cm
  if (distance <= 20) {
    // Turn on the LED if the distance is less than or equal to 20 cm
    digitalWrite(ledPin, HIGH);
  } else {
    // Turn off the LED if the distance is greater than 20 cm
    digitalWrite(ledPin, LOW);
  }

  // Wait for a short period before next measurement
  delay(500);  // Adjust delay as necessary
}

iii. simulation

After pasting the codes, i had clicked on “serial monitor” and clicked on ‘simulation button’ and clicked near ultrasonic distance sensor and drag to certain distance, making to glow the LED.I have screen recorded my simulation.

Then, clicked near ultrasonic distance sensor and drag to certain distance, making to glow the LED.

Verification of simulation using Arduino IDE.

To verify my simulation, i thought of testing using ‘Arduino IDE’. So i downloaded it from this link

Arduino IDE download image

Then if did the connections using the components required.

wiring for atmega test

Opened the Arduino IDE and selcted a ‘new sketch’ under the ‘File” and delated all as we wanted to paste the code from the chatGPT.

atmega test open

code paste 1. we had to go to Tools > Boards > Arduino UNO to select our board.

board select image 2. Next go to Tools > Port > COM7 to select our port.

  1. clicked on “Compile/tick” to compile the code followed by clicking on “Upload” to upload the codes to Arduino.

compile and upload image

Once done with uploading, go to Tools > Serial monitor or click on ‘Serial Monitor’ at upper right corner.

2. Using WOKWI for simulation using ESP-WROOM-32.

About ESP-WROOM-32

It is a powerful, generic Wi-Fi + BT + BLE-MCU module that targets a wide variety of applications, which range from low-power sensor networks to the most demanding tasks. It has ‘ESP32-D0WDQ6’ mebedded chip designed to be scalable and adaptive.It contains two CPU cores which can be controoled individually.It is developed by Espressif systems. source reference from here.

specifications using datasheet

- Wifi- 802.11n upto 150 Mbps, 2.4GHz~2.5GHz.

- Bluetooth - v4.2 BR/EDR and BLE.

- Hardware - operating voltage 2.7~3.6v and current 80mA.

- CPU- Xtensa® single-/dual-core 32-bit LX6 microprocessor(s)

- Internal memory- 448kB ROM and 520 kB 0f 0n-chip SRAM.

- External Flash and SRAM- four 16-MB esternal QSPI flash and SRAM with hardware encryption.

- Pin description-has 38pins, pins GPIO6 TO GPIO 11 are connected to the integrated SPI flash.Each of strapping pins are connected with its internal pull-up/pull-down.

Pinout for ESP32

ESP32 pin out image

source image from here

For this simulation, i have used ESP32 board, Ultrasonic Distance sensor and LED along with jumper wires. I referred the connection ideas from the chatGPT. I went to the browser and opened WOKWI from this link and choosed ‘ESP32” as my board. I delated the code space.

esp32 image1

i had added Ultrasonic distance sensor, LED, resistor by clicking the ‘+’ sign near the simulation button and clicked ‘R’ on keyboard to rotate the components.

esp32image2

i have done the connections of components to ESP32 board by referring the ideas from chatGPT.

  1. ultrasonic Distance sensor-

    • ‘ucc’ to 5v on ESP32 board.
    • ‘Gnd’ to GND on ESP32 board.
    • ‘trig’ to GPIO 18 on ESP32 board.
    • ‘Echo’ to GPIO 19 on ESP32 board.
  2. LED - anode to GP10 23 and cathode to GND through 220 ohm resistor.

esp32wiringimage3

Next, i copied the codes shown by chatGPT and pasted to the code space.

Codes:

const int trigPin = 18;  // Trigger pin of the ultrasonic sensor
const int echoPin = 19;  // Echo pin of the ultrasonic sensor
const int ledPin = 23;   // LED pin

long duration;  // Variable to store the duration of the pulse
int distance;   // Variable to store the calculated distance

void setup() {
  // Start serial communication for debugging
  Serial.begin(115200);  // ESP32 uses 115200 baud rate by default

  // Set up the ultrasonic sensor pins
  pinMode(trigPin, OUTPUT); // Set trig pin as an output
  pinMode(echoPin, INPUT);  // Set echo pin as an input

  // Set up the LED pin
  pinMode(ledPin, OUTPUT);  // Set LED pin as an output
}

void loop() {
  // Clear the trigPin by setting it LOW:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);  // Wait for 2 ms to ensure clean signal

  // Set the trigPin HIGH for 10 microseconds to send the pulse
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the duration of the echo pulse
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance in cm (Speed of sound: 343 m/s)
  distance = (duration / 2) * 0.0343;

  // Print the distance to the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Check if the distance is less than or equal to 20 cm
  if (distance <= 20) {
    // Turn on the LED if the distance is less than or equal to 20 cm
    digitalWrite(ledPin, HIGH);
  } else {
    // Turn off the LED if the distance is greater than 20 cm
    digitalWrite(ledPin, LOW);
  }

  // Wait for a short period before next measurement
  delay(500);  // Adjust delay as necessary
}

codepaste image4

Then, i clicked on simulation button and it started compiling which took some time.

esp32simulate image5

After finishing compiling, it showed distance range. I clicked on Ultrasonic distance sensor and dragged to the range of 20cm.The LED glow when its range 20cm and below.I stopped my simulation.

esp32simulation image 6

3.Simulation using WOKWI for RP2040 microcontroller.

My refernce link for RP2040 datasheet is here.

RP2040 is a microcontroller by Raspberry Pi and it is a dual- core ARM Cortex-MO+ microcontroller. It operates at a clock speed of 133 MHz and is equiped with 264KB od SRAM and 2 MB of flash memory.It is mostly used with Programming environments like MicroPython and C/C++. It is low-cost, high performance microcontroller device with flexible digital interfaces.

Why this Chip is called RP2040? By referring my datasheet source above, the post-fixed numeral on RP2040 comes from the following:

abbraviation of RP2040

image is screenshoot from my referred datasheet from above link

specifications:

- has Dual Cortex MO+ processor cores, upto 133 MHz.
- has 264 bB of embedded SRAM in 6 blanks.
- 30 multifuncton GPIO
- 6 dedicated IO for SPI flash(supporting XIP)
- dedicated hardware for commonly used peripherals.
- 4 channel ADC with internal temperature sensor.
- USB 1.1 Host/Device.

**Pin Out for RP2040

pin out image rp2040

For this simulation, i have used RP2040, Ultrasonic Distance sensor and LED along with jumper wires. I referred the connection ideas from the chatGPT. I went to the browser and opened WOKWI from this link and choosed “Micropython” . I delated the code space.

rp2040 components add

i had added Ultrasonic distance sensor, LED, resistor by clicking the ‘+’ sign near the simulation button and clicked ‘R’ on keyboard to rotate the components.

i have done the connections of components to ESP32 board by referring the ideas from chatGPT.

refer from chatGPT 1. ultrasonic Distance sensor-

- 'ucc' to 3.3V on RP2040 board.
- 'Gnd' to GND D1
- 'trig' to GPIO 15
- 'Echo' to GPIO 14
  1. LED - anode to GP10 13 and cathode to GND through 220 ohm resistor.

connection

Next, i copied the codes shown by chatGPT and pasted to the code space.

Codes pasted from chatGPT:

 # Set up the ultrasonic sensor pins
trigger_pin = Pin(15, Pin.OUT)
echo_pin = Pin(14, Pin.IN)

# Set up the LED pin
led_pin = Pin(13, Pin.OUT)

def get_distance():
    # Send a pulse to trigger the ultrasonic sensor
    trigger_pin.high()
    time.sleep_us(10)  # Pulse width for 10 microseconds
    trigger_pin.low()

    # Wait for the echo pin to go high and calculate the time taken for echo to return
    while echo_pin.value() == 0:
        pulse_start = time.ticks_us()

    while echo_pin.value() == 1:
        pulse_end = time.ticks_us()

    # Calculate the distance (time / 2) * speed of sound (343 m/s = 0.0343 cm/us)
    pulse_duration = time.ticks_diff(pulse_end, pulse_start)
    distance = (pulse_duration * 0.0343) / 2  # Distance in centimeters
    return distance

def control_led_based_on_distance():
    distance = get_distance()
    print("Distance:", distance, "cm")

    # If the distance is less than 20 cm, turn on the LED
    if distance < 20:
        led_pin.high()
    else:
        led_pin.low()

# Set up a timer to run the control_led_based_on_distance function every 1 second
timer = Timer()
timer.init(period=1000, mode=Timer.PERIODIC, callback=lambda t: control_led_based_on_distance())

# Main loop (empty, since everything runs on the timer)
while True:
    pass

Then, i clicked on simulation button and it started compiling which took some time.After finishing compiling, it showed distance range. I clicked on Ultrasonic distance sensor and dragged to the range of 20cm.The LED glow when its range 20cm and below.I stopped my simulation.

simulate image

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 ESP32 S 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 ATmega328P chip with components like servo motor and ultrasonic distance sensor for Automatic Hand Sanitizer Dispenser as my project does not need Wi-Fi/bluetooth connections and does not require more power consumption.

Disclaimer: the codes and connections ideas are from chatGPT