5. Embedded programming

For this week, general information about microcontrollers will be delivered to be used for out final project after completion of all outcomes for this program.

Link for group assignment.

First Individual assignment

FIrst task for this week was to apply the blink feature to a XIO-ESP32_S3 microcontroller by following the steps below:

(followed from seeed official website)

Step 1. Download Arduino IDE



alt text

Note: click on just download to access the free version of the software

alt text


https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json


Copy the link in the Additional Boards Manager URLs located at File > Preferences



alt text

Step 3. Open Tools > Board > Boards Manager…, enter “esp32” into the search bar, pick the most recent esp32 version, and install it.


Note: choose ESP32 by Espressif systems as followed from the official Seeed systems.

alt text


Step 4. Select your board and port

Board

Click Tools > Board > ESP32 Arduino, then choose “XIAO_ESP32S3”. To get to the bottom of the somewhat lengthy list of boards, you must roll.

alt text


Note: the microcontroller type is XIAO-ESP32-S3. Therefore, why the board is set up according to it.


alt text


Port

Choose the serial port name of the linked XIAO-ESP32-S3 by going to Tools > Port. Since COM1 and COM2 are typically designated for hardware serial ports, this is most likely COM3 or higher.



alt text


Step 5. Using a USB Type-C connection, connect the XIAO ESP32S3 to your computer.


Step 6.

The code below will be used for testing the blinking function.



alt text


Step 7.

Click on upload to deliver code commands to the XIAO-ESP32-S3.


Recording below is the microcontroller’s response to the code.



https://youtube.com/shorts/YXZb71ilT4s?si=defWhvwLyXT_dxEw


Second individual task

For this task, the microcontroller will be programmed according to the commands assigned in the PDF below.

Embeded Programing Activities

What is a programming environment?

A programming environment is a collection of resources and tools that simplify the developing, testing, and debugging of software. Included are a code editor, compiler or interpreter, debugger, and occasionally additional capabilities such as build automation and version control. Examples of Integrated Development Environments (IDEs) that provide an organized workspace to simplify project administration and coding are Visual Studio Code and Linux.

Part one

The LED blink function will be implemented again, but the delay times will be set up randomly between one to five seconds.

Step 1

Use the random function

Random(min,max)


/*

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(1000);                      // wait for a second
}


Min is the lowest variable possible of the random value and is an optional statement. Meanwhile, Max is the random value’s upper bound and must be set.


Proof of code working by video recording the microcontroller’s blinks.




Part two

The microcontroller will be set up to deliver a one word message in morse code using the blinking function.


The word showcased by the LED is “SALAM”


/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(3000);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(3000);                      // wait for a second
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(3000);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(3000);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(3000);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(4000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(3000);
}


Recording of blinking resembling the word “SALAM” can be viewed below.


After completing the second task, a microcontroller of the type Bluefruit Sense (nRF52840) will be used to apply the blinking function using a new software called Mu Editor.


Third individual task

In this task, a code will be run using a software called Thonny.



alt text


Software’s website



alt text


Note: recommended option was downloaded for this task.


Code below is used for applying blink feature using Thonny.


alt text



Clip of blink feaure using Thonny is shown below:




Fourth individual task

Completing previous exercises using Thonny from the Embeded Programing Activities PDF



Part one

The LED blink function will be implemented again, but the delay times will be set up randomly between one to five seconds.


Note: same steps from the second task will be used.


Code for part one is shown below.



import machine
import time
import random

# Configure the GPIO pin for the LED
led_pin = machine.Pin(13, machine.Pin.OUT)  # Replace 15 with your GPIO pin number

# Function to blink the LED with random intervals
def random_blink(min_delay, max_delay):
    while True:
        led_pin.on()  # Turn LED on
        time.sleep(random.uniform(min_delay, max_delay))  # Random delay
        led_pin.off()  # Turn LED off
        time.sleep(random.uniform(min_delay, max_delay))  # Random delay

# Run the random blinking function with delays between one and five seconds
random_blink(1.0, 5.0)


Recording of the part is shown below.




Part two

The microcontroller will be set up to deliver a one word message in morse code using the blinking function.


Code for this part is shown below.


import machine
import time

# Configure the GPIO pin for the LED
led_pin = machine.Pin(13, machine.Pin.OUT)  # Replace 15 with your GPIO pin number

# Morse code dictionary
MORSE_CODE = {
    'K': '-.-',
    'H': '....',
    'A': '.-',
    'L': '.-..',
    'E': '.',
    'D': '-..'
}

# Function to blink LED for a dot
def dot():
    led_pin.on()
    time.sleep(1)  # Dot duration (1 second)
    led_pin.off()
    time.sleep(0.5)  # Pause between dots and dashes

# Function to blink LED for a dash
def dash():
    led_pin.on()
    time.sleep(2)  # Dash duration (2 seconds)
    led_pin.off()
    time.sleep(0.5)  # Pause between dots and dashes for half a second

# Function to blink LED for a character in Morse code
def blink_character(character):
    code = MORSE_CODE.get(character.upper(), '')  # Get Morse code for the character
    for symbol in code:
        if symbol == '.':
            dot()
        elif symbol == '-':
            dash()
    time.sleep(3)  # Pause between letters for three seconds

# Function to blink the word "KHALED"
def blink_word(word):
    for char in word:
        blink_character(char)
        time.sleep(1.0)  # Pause between words

# Run the Morse code blinking function
blink_word("KHALED")



Recording for this part is shown below.




Individual task No. 5

Create beeping sounds using Arduino

Code below was applied to the buzzer used to create the desired beeping sounds as an example of an application.

// Define the buzzer pin
#define BUZZER_PIN 13

void setup() {
  // No setup is needed for tone function
}

void loop() {
  // Generate a 1 kHz beep for 500 ms
  tone(13, 1000, 500);

  // Wait for 1 second
  delay(1000);

  // Generate a 2 kHz beep for 300 ms
  tone(13, 2000, 300);

  // Wait for 1 second
  delay(1000);
}



Activating 130-DC fan motor using Arduino

This part wil cover the coding steps for activating a 130-DC fan motor which were extracted from an this website and it will be uploaded to Arduino.




 /* 130 DC Motor
     by DFRobot <https:www.dfrobot.com>

    */

    int motorPin = 13;   //Motor drive pin D3
    int motorSpeed;     //Define motor speed

    void setup()
    {
      Serial.begin(9600);
    }

    void loop()
    {

      for(motorSpeed = 0 ; motorSpeed <= 255; motorSpeed+=5)
      {
        analogWrite(motorPin, motorSpeed);   //PWM speed control
        delay(30);
      }
      for(motorSpeed = 255 ; motorSpeed >= 0; motorSpeed-=5)
      {
        analogWrite(motorPin, motorSpeed);   //PWM speed control
        delay(30);
      }
    }

Adjusting an RGB light using Arduino