Skip to content

Week 06. Embedded Programming

Embedded programming is a type of programming where we write code to control electronic devices.

Group Assignment:

  • Browse through the datasheet for your microcontroller
  • Compare the performance and development workflows for other architectures
  • Document your work to the group work page and reflect on your individual page what you learned

Group Assignment Link

Takeaways: key points from the RP2040 datasheet
- Dual Processor Power: Think of the RP2040 as having two brains (Cortex-M0+ cores) that work together to handle tasks faster, up to 133 million times per second. - Plenty of Memory: It has a good amount of memory (264 kB of SRAM) to store and process data. It’s like having several notebooks to write things down and refer back to them quickly. - Flexible Pins for Connecting: There are 30 pins that can be used in different ways to connect to other devices like sensors and displays. It’s like having many different types of jacks to plug things into. - Specialized Support for Common Devices: The RP2040 has built-in support for common devices like displays and memory cards. This makes it easier to connect them without needing extra components. - Customizable Connections and Analog Sensing: You can create your own ways to connect devices using programmable IO (PIO). Plus, it can measure analog signals like temperature accurately and quickly using its built-in ADC. - Low-Cost, High-Performance: The RP2040 is designed to be affordable while still being powerful enough to handle a variety of tasks.

Individual Assignment:

  • Write a program for a microcontroller development board to interact (with local input &/or output devices) and communicate (with remote wired or wireless devices)

Disclaimer: All the Assignments and Documentations are done and written by me, however after writing it myself, some I pass it through chatGPT to fix my language and grammar.

Arduino Uno

Arduino Uno Rev. 3 Microcontroller Board is based on the Microchip Technology ATmega328 8-bit Microcontroller (MCU). Arduino Uno features 14 digital input/output pins (six of which can be used as PWM outputs), six analog inputs, and a 16MHz quartz crystal.

Features

  • Microchip Technology ATmega328 8-Bit AVR Microcontroller
  • 5V operating voltage
  • 6V to 20V input voltage (limit)
  • 14 (with 6 providing PWM output) digital I/O pins
  • Six analog input pins
  • 20mA DC current per I/O pin
  • 50mA DC current for 3.3V pin
  • 32KB Flash memory (ATmega328P) (0.5KB used by bootloader)
  • 2KB SRAM (ATmega328P)
  • 1KB EEPROM (ATmega328P)
  • 16MHz clock speed
  • 68.6mm x 53.4mm (Length x Width)
  • 25g weight
  • Powered via USB connection or with external power supply
  • Includes a number of facilities for communicating with a computer, another Uno board, or other MCUs
    • ATmega328 provides UART TTL (5V) serial communication, available on digital pins 0 (RX) and 1 (TX)

For further information go through Datasheet

Programming

Basic Arduino Programming Structure

//Edited from chatGPT
// This is a comment. It helps to understand code.
// Comments are ignored by the Arduino compiler.

// Setup function runs once when power up or reset the Arduino board.
void setup() {
  // Initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
}

// Loop function runs repeatedly after the setup function.
void loop() {
  // Turn on the LED connected to digital pin 13.
  digitalWrite(13, HIGH);  // HIGH is for turning the pin ON.
  delay(1000);  // Wait for 1 second (1000 milliseconds).

  // Turn off the LED.
  digitalWrite(13, LOW);  // LOW is for turning the pin OFF.
  delay(1000);  // Wait for 1 second.
}

Explanation:

  • Comments: are there to help you and others understand what does or going on with code.
  • Setup Function: It’s where you get everything ready before the main logic starts. You use it to set up things like turning on lights, organizing your tools, etc.
  • Loop Function: Once the setup defined, the loop function keeps repeating agin ang again based on provided task, until the power is turned off.
  • pinMode(): This function is for telling what each pin does. It’s like deciding whether a switch will turn on a light or make a noise when pressed.
  • digitalWrite(): When digitalWrite(1, HIGH), it turn something ON, and when digitalWrite(1, LOW) it turn off something.
  • delay(): This function is like hitting the pause button.

I wrote a code using the Arduino IDE to make an LED on and off. This code works in such a way that when the command ‘1’ is received via the serial monitor, the LED turns on, and when ‘0’ is received, it turns off. After writing the code, I utilized Tinkercad to simulate the functionality, ensuring its correctness. - The Code I used:

// program to control LED through serial commnucation
//LED connected to Pin 5
int led=5;
//initialise character for storing data from computer
char data;
void setup() {
  // initialise serial comm at 9600 baud rate
  Serial.begin(9600);
   //initialise pin 5 as output
  pinMode(led, OUTPUT);

}

void loop() {
  // check if data has been received in the serial buffer
  if(Serial.available()>0)
  {
 //then read the serial buffer and store in a variable
  data=Serial.read();
  }
  //if the character received is 1, then turn ON the LED
  //else if the character received is 0, then turn OFF lED
  if(data == '1')
  {
   digitalWrite(led, HIGH);
   Serial.println("LED is ON");
   delay(500);
  }
  else if (data == '0')
  {
    digitalWrite(led,LOW);
    Serial.println("LED is OFF");
    delay(500);
  }
}
  • Once satisfied with the simulation results, I proceeded to connect the Arduino Uno board to my computer via USB. Following this, I selected the appropriate board (Arduino Uno) and COM port within the Arduino IDE.
  • After compiling the code, I uploaded it to the Arduino Uno board. The process went smoothly, confirming the successful operation of the program.

ESP32 NodeMCU

The NodeMCU development board based on ESP32, features WiFi+Bluetooth connectivity, onboard CH340 and keys. What’s more, all the I/O pins of ESP32-S module are accessible via the extension headers.

Board Features:-

  • Onboard ESP32-S module
  • Onboard CH340, USB to UART converter
  • USB port for power input, firmware programming, or UART debugging
  • 2x19pin extension headers, breakout all the I/O pins of the module
  • 2x keys, used as reset or user-defined

image from this source

Board Specifications:-

  • WIFI module: ESP32-S
    • Processor: ESP32
  • Built-in Flash: 32Mbit
  • Antenna: Onboard PCB antenna
  • Peripheral interface: UART/GPIO/ADC/DAC/SDIO/PWM/I2C/I2S
  • WiFi protocol: IEEE 802.11 b/g/n
  • Bluetooth: Bluetooth 4.2
  • Frequency range: 2.4G ~ 2.5G (2400M ~ 2483.5M)
  • WIFI mode: Station / SoftAP / SoftAP+Station
  • Power supply: 5V
  • Logic level: 3.3V
  • Dimensions: 48.26mm x 25.4mm

To know more about Development Board you can visit Datasheet

Programming

  • For the second microcontroller board development test, I opted for the ESP32 NodeMCU to enable remote control of an LED via a WiFi access point using a web browser. Initially, I installed the ESP32 board manager by navigating to preferences and adding the ESP32 board manager.
  • After installing the ESP board manager, I proceeded by connecting the ESP development board to my computer using a USB cable. I ensured that the correct COM port was selected in the Arduino IDE. Additionally, I configured the ESP tool as the programmer for the board.
  • Next, I utilized the WiFiAccessPoint code by Elochukwu Ifediora, compiled it, and uploaded it onto the ESP32 NodeMCU board.
  • This is the code I used:
/*
  WiFiAccessPoint.ino creates a WiFi access point and provides a web server on it.

  Steps:
  1. Connect to the access point "yourAp"
  2. Point your web browser to http://192.168.4.1/H to turn the LED on or http://192.168.4.1/L to turn it off
     OR
     Run raw TCP "GET /H" and "GET /L" on PuTTY terminal with 192.168.4.1 as IP address and 80 as port

  Created for arduino-esp32 on 04 July, 2018
  by Elochukwu Ifediora (fedy0)
*/

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>

#define LED_BUILTIN 2   // Set the GPIO pin where you connected your test LED or comment this line out if your dev board has a built-in LED

// Set these to your desired credentials.
const char *ssid = "yourAP";
const char *password = "yourPassword";

WiFiServer server(80);


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(115200);
  Serial.println();
  Serial.println("Configuring access point...");

  // You can remove the password parameter if you want the AP to be open.
  // a valid password must have more than 7 characters
  if (!WiFi.softAP(ssid, password)) {
    log_e("Soft AP creation failed.");
    while(1);
  }
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.begin();

  Serial.println("Server started");
}

void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> to turn ON the LED.<br>");
            client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(LED_BUILTIN, HIGH);               // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(LED_BUILTIN, LOW);                // GET /L turns the LED off
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}
  • Upon completion, I searched for the WiFi network named ‘yourAP’ and successfully connected to it.
  • I went to chrome and navigated to the designated URL where commands for toggling the LED ON and OFF were available. Upon clicking the respective commands, the LED responded accordingly.

QUENTORRES

The original programmer created by Quentin Bolsée and redesigned by Adrián Torres. Quentorres is born at the 2024 Instructors Bootcamp held in León.

Servo using QUENTORRES

  • For the third microcontroller test, I employed Quentorres which is powered by the Raspberry Pi 2040 chip, offering dual-core processing up to 133 MHz, 264KB of SRAM, and 2MB of onboard flash memory. Go through Datasheet to know more about RP2040
  • I constructed this board during the electronics production week. Initially, I modified the code available in the examples to suit my requirements.
/* Sweep
  by BARRAGAN <http://barraganstudio.com>
  This example code is in the public domain.

  modified 28 May 2015
  by Michael C. Miller
  modified 8 Nov 2013
  by Scott Fitzgerald

  http://arduino.cc/en/Tutorial/Sweep
*/
//Modified by Sangay Dorji
//Fab Academy 2024 -JNWSFL
//Servo
// Fab Acdemy


#include <Servo.h>

Servo myServo;  // This is to create servo object to control a servo motor
void setup() {
  myServo.attach(28);  // my servo on GIO28 
  Serial.begin(115200);
}

void loop() {
  if(Serial.available()){
    int angle = Serial.parseInt();
    myServo.write(angle);
  }
  delay(20);
  }
  • Then, I ensured that I selected the right board and correct COM port in the Arduino IDE.
  • After this, I compiled the modified code and uploaded it to the Quentorres board. The code facilitated servo movement up to 180 degrees based on the input provided via the serial monitor. Additionally, I adjusted the settings in the serial monitor to ensure that “No Line Ending” was selected.

Files for the Week

codes folder