Skip to content

4. Embedded Programming

Group assignment for week 4: - Demonstrate and compare the toolchains and development workflows for available embedded architectures

Basics of Microcontroller

This is the link from where we explore about Von Neumann and Harward Architecture.

Von Neumann Architecture vs Harvard Architecture

Difference between Von Neumann and Harvard Architecture

Feature Von Neumann Architecture Harvard Architecture
Description Ancient computer architecture based on the stored program computer concept. Modern computer architecture based on the Harvard Mark I relay-based model.
Memory Address Same physical memory address is used for instructions and data. Separate physical memory addresses are used for instructions and data.
Bus Structure Common bus for data and instruction transfer. Separate buses are used for transferring data and instructions.
Instruction Execution Two clock cycles are required to execute a single instruction. An instruction is executed in a single cycle.
Cost Cheaper in cost. Costlier than Von Neumann Architecture.
CPU Access CPU cannot access instructions and read/write at the same time. CPU can access instructions and read/write at the same time.
Applications Used in personal computers and small computers. Used in microcontrollers and signal processing.

Difference between Microprocessor and Microcontroller

Here is link of the tutorial we reffered which highlights what is microprocessor and microcontroller.

A microcontroller is a small computer on a single chip, containing a processor core (or cores), operational memory (RAM) and electrically erasable programmable read-only memory (EEPROM) for program memory.

Microprocessors are the predominant type of computer processor, integrating all the required components of a CPU into a single circuit. This integrated design improves reliability by reducing potential points of failure. While microprocessors do not contain on-chip program memory, they are optimized for demanding general-purpose use and designed to support and be supported by specialized computer peripherals.

The key difference between these two units is that microcontrollers combine all the necessary elements of a microcomputer system onto a single piece of hardware. Microcontrollers do not require additional peripherals or complex operating systems to function, while microprocessors do. Both circuits contain CPUs, however, microcontrollers also integrate memory, input/output (I/O) components and other varied peripherals.

Exploring Data sheet of four microcontroller board available in our lab

The board are list below:

a. XIAO RP2040

b. Arduino UNO R3

c. XIOA

d. Adafruit circuit playground

a. XIAO RP2040

Data sheet link

The Seeed Studio XIAO RP2040 is as small as the Seeed Studio XIAO SAMD21 but it's more powerful. On one hand, it carries the powerful Dual-core RP2040 processor that can flexible clock running up to 133 MHz which is a low-power microcontrollers. On the Seeed Studio XIAO RP2040 there is also 264KB of SRAM, and 2MB of on-board Flash memory which can provide more program to save and run. On the other hand, this little board has good performance in processing but needs less power. All in all, it is designed in a tiny size as small as a thumb(21x17.8mm) and can be used for wearable devices and small projects.

Specifications:

i. CPU: Dual-core ARM Cortex M0+ processor up to 133MHz

ii. Flash Memory: 2MB

iii. SRAM: 264KB

iv. Digital I/O Pins: 11

v. Analog I/O Pins: 4

vi. PWM Pins: 11

v. I2C interface: 1

vi. SPI interface: 1

vii. UART interface: 1

viii. Power supply and downloading interface: Type-C

ix. Power: 3.3V/5V DC

x. Dimensions: 21×17.8×3.5mm

Pinout

Supported Development Environments

i. Arduino IDE

ii. MicroPython

iii. CircuitPython

iv. C/C++ SDK (Raspberry Pi Pico SDK)

b. Arduino UNO R3

i.Introducton:

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

c. ESP WROOM 32

ESP32-WROOM-32 is a powerful, generic Wi-Fi + Bluetooth® + Bluetooth LE MCU module that targets a wide variety of applications, ranging from low-power sensor networks to the most demanding tasks, such as voice encoding, music streaming and MP3 decoding.

Specifications

  • Power Consumption

  • Operating Voltage: 3.0V to 3.6V

  • Ultra-low power modes

  • Deep Sleep Current: ~10 µA

  • Core

    • Processor: Xtensa® 32-bit LX6 dual-core (or single-core option)

    • Clock Speed: Up to 240 MHz

    • RAM: 520 KB SRAM

    • ROM: 448 KB

  • Wireless Connectivity

    • Wi-Fi: 802.11 b/g/n, 2.4 GHz

    • Bluetooth: v4.2 BR/EDR and BLE

  • Memory

  • Flash: 4 MB (standard) or 8/16 MB options

  • External SPI Flash & SRAM support

  • GPIO & Interfaces

The module has 38 pins

Up to 36 GPIO pins

  • ADC: 12-bit, 18 channels

  • DAC: 8-bit, 2 channels

  • Communication Interfaces: UART, SPI, I2C, I2S, PWM, SDIO

  • Touch Sensor: 10 capacitive touch pins

  • Peripherals

    • PWM for motor and LED control

    • Integrated Hall sensor

    • Temperature sensor

    • RTC (Real-Time Clock)

  • Programming Language

    • Arduino and Micro Python

Pinout reference

esp32pin

d. Adafruit circuit playground

Intro: Specification: Pinout: Architecture:

Exploring toolchains and development workflows for some development board available in our lab

Getting stared with Arduino UNO R3

For this group work, we have decided to use ultrasonic sensor, buzzer and LED along with a resistor, jumper wires and breadboard, by using Arduino UNO as board. At the end, we expected the ultrasonic sensor detects objects from distance of 20cm letting LED to turn on and buzzer to sound and vice-versa when object is away.

We have downloaded Arduino IDE from here

Steps involved for this work:

i. Wiring the components

  1. ultrasonic sensor- 'ucc' to 5v on Arduino board, - 'Gnd' to GND on Arduino board - 'trig' to pin 10 - 'Echo' to pin 9

  2. LED - anode to pin 13 and cathode to GND through resistor.

  3. Buzzer - positive to pin 2 and negative to GND

wiring components

ii. Writing the Arduino Code.

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.

arduino ide new file open

code pasted

codes below:

const int trigPin = 10;     // Trigger pin for ultrasonic sensor
const int echoPin = 9;      // Echo pin for ultrasonic sensor
const int ledPin = 13;      // LED pin
const int buzzerPin = 2;    // Buzzer pin

void setup() {
  // Initialize pins as OUTPUT or INPUT
  pinMode(trigPin, OUTPUT);    // Trigger pin for ultrasonic sensor
  pinMode(echoPin, INPUT);     // Echo pin for ultrasonic sensor
  pinMode(ledPin, OUTPUT);     // LED pin
  pinMode(buzzerPin, OUTPUT);  // Buzzer pin

  // Start serial communication
  Serial.begin(9600);
}

void loop() {
  long duration, distance;

  // Send a pulse to the ultrasonic sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the time for the echo to return
  duration = pulseIn(echoPin, HIGH);

  // Calculate distance in centimeters
  distance = (duration / 2) / 29.1;  // Speed of sound is 343 m/s

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

  // Check if the object is within 20 cm range
  if (distance < 20) {
    digitalWrite(ledPin, HIGH);   // Turn ON the LED
    digitalWrite(buzzerPin, HIGH); // Turn ON the Buzzer
  } else {
    digitalWrite(ledPin, LOW);    // Turn OFF the LED
    digitalWrite(buzzerPin, LOW); // Turn OFF the Buzzer
  }

  delay(500);  // Wait for half a second before measuring again
}
iii. Compiling and Uploading the code

We had to connect our Arduino board to the computer using USB port. 1. we had to go to Tools > Boards > Arduino UNO to select our board.

  1. Next go to Tools > Port > COM7 to select our port.

board and port select

  1. clicked on "Compile/tick" to compile the code.

  2. clicked on "Upload" to upload the codes to Arduino.

compile and upload image

iv. Testing the result

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

serial monitor and final result

Disclaimer : codes and ideas are referred from chatGPT for this work

Getting started with ESP32

We used the same component and code as above.

Open the Arduino IDE. For ESP 32, we firsltly installed the board by adding the board uing the URL:https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json

Follow the step as shwon in the image below.

alt text

Next we select the board and port.

alt text

alt text

We need to select a programmer called esptool programming the board using Arduino IDE.

alt text

Used the code below.

XIAO RP2040

Adafruit Circuit playground

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

  1. Program in memory(kb)- 256 Kb

  2. Data memory(Kb)- 32 Kb

  3. No of pins- 48

  4. Packages- TQFP, QFN

  5. USB- Yes

  6. Sercom- 6

  7. TCC- 3

  8. I2S- Yes

  9. RTC- Yes

  10. WDT- Yes

  11. Digital Input/ Output pins- 11

  12. Analog Input/ output pins- 11

  13. DAC- yes

  14. PTC- 120/10

  15. Operating voltage- 3.3v

  16. Processor- Arm Cortex -MO+

  17. Power Consumption- 10.5 µA to 11.3 mA

  18. On board LEDs- power LED and Status LED

Pin out reference

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.

Image1

Next I opened the CPLAYBOOT(E) and pasted the program file which I had copied and then program start executing.

Image2

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