Skip to content
On this page

Embedded programing

This week I have mainly studied about how to use Arduino UNO. I have browse through the Datasheet of my Arduino UNO and learned programmed a microcontroller board. I have also compared the Arduino UNO and the Raspberry Pi arcchitecture.

Features & Layout

The Arduino UNO uses Atmega328P-PU as core MCU, the key features is listed below.

  • Name: Arduino UNO
  • Microcontroller: Atmega328P-PU
  • Operating Voltage: 5V
  • Input Voltage: 7-20V
  • Number of GPIO Pins: 20
  • Digital Pins: 14
  • PWM Pins: 6
  • Analog Input Pins: 6
  • I2C Ports: 1
  • UART Ports: 1
  • SPPI Ports: 1
  • Flash Memory: 32 KB of which 0.5 KB used by bootloader
  • SRAM: 2 KB
  • EEPROM: 1 KB
  • Clock Speed: 16 MHz

The overall layout of Arduino UNO the broad is shown as below.

Software

We can download Arduino IDE from [www.arduino.cc] (https://www.arduino.cc/) according to your PC's version and then install by yourself.

Design and Simulation

TinkerCAD is a great website where you can design and test your circuit online.

Light Control Experiment

We can use switch to control the circuit, in this case we use pin 7 as input port.

The code for this control loop is as below.

const int LED1=10;
const int LED2=13;  

int val=0;
  
void setup()
{
  pinMode(LED1, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(7, INPUT);
}

void loop(){
val=digitalRead(7);
  if(val==HIGH)
{
   digitalWrite(LED1,HIGH);
   digitalWrite(LED2,LOW);
}
else
{
   digitalWrite(LED2,HIGH);
   digitalWrite(LED1,LOW);
}
delay(1000);
}

Servo Control with Ultrasonic Sensor Input

I have first designed the circuit online then write the code and deployed on it.
#include <Servo.h>
#define EchoPin A1
#define TrigPin A0  


Servo myservo;  
int count = 0;
int val;    
long duration;


void setup() {
  myservo.attach(9); 
  Serial.begin(115200);
  pinMode(TrigPin, OUTPUT);
  pinMode(EchoPin, INPUT);
  digitalWrite(TrigPin, LOW);
  delay(1);
}

void loop() {

  Serial.print(count++);
  Serial.println("");
  Serial.println(getDistance());
  Serial.println("");
  val=getDistance();   

  if (val<1023){
  val = map(val, 0, 1023, 0, 180);     
  myservo.write(val);                  
  delay(15);
  }
  return;

}


long getDistance() {
    // trig
    digitalWrite(TrigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(TrigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(TrigPin, LOW);
    // echo
    duration = pulseIn(EchoPin, HIGH);     // unit: us
    return duration * 0.34029 / 2;         // unit: mm
}

Final result is that when an obstacle is approching near to the Ultrasonic Sensor then servo motor will rotate 180 degree clockwise, when the obstacle leaves the servo motor will rotate 180 degree anticlockwise.

Try Raspberry Pi 4B - as group assignment

In order to compare the performance and development workflows for other architectures, this time I use Raspberry Pi 4B to light up two LED light, the circuit is connected as below.

The Raspberry Pi 4B I uses as core MCU, the key features on data sheet is listed below.

  • Broadcom BCM2711, Quad core Cortex-A72 (ARM v8) 64-bit SoC @ 1.5GHz
  • 2GB, 4GB or 8GB LPDDR4-3200 SDRAM (depending on model)
  • 2.4 GHz and 5.0 GHz IEEE 802.11ac wireless, Bluetooth 5.0, BLE
  • Gigabit Ethernet
  • 2 USB 3.0 ports; 2 USB 2.0 ports.
  • Raspberry Pi standard 40 pin GPIO header (fully backwards compatible with previous boards)
  • 2 × micro-HDMI ports (up to 4kp60 supported)
  • 2-lane MIPI DSI display port
  • 2-lane MIPI CSI camera port
  • 4-pole stereo audio and composite video port
  • H.265 (4kp60 decode), H264 (1080p60 decode, 1080p30 encode)
  • OpenGL ES 3.1, Vulkan 1.0
  • Micro-SD card slot for loading operating system and data storage
  • 5V DC via USB-C connector (minimum 3A*)
  • 5V DC via GPIO header (minimum 3A*)
  • Power over Ethernet (PoE) enabled (requires separate PoE HAT)
  • Operating temperature: 0 – 50 degrees C ambient

On Raspberry Pi I have used Python language to program and what I have done is simply shine a LED light with my Raspberry Pi as below.

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
print ("LED1 on")
GPIO.output(18,GPIO.HIGH)
time.sleep(1)
print ("LED1 off")
GPIO.output(18,GPIO.LOW)
print ("LED2 on")
GPIO.output(15,GPIO.HIGH)
time.sleep(1)
print ("LED2 off")
GPIO.output(15,GPIO.LOW)

The result is shown as video below.