Appearance
Embedded programming
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 architecture.
Features & Layout for both architecture
Arduino UNO
The Arduino UNO uses Atmega328P-PU as core MCU, the key features & layouts 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 detailed PCB layout of Arduino UNO the broad is shown as below.
ATmega328P is the main processor of Arduino Uno. It is a high performance, low power AVR 8-bit microcontroller, with advanced RISC architecture.Raspberry Pi 4 Model B
The key features & layouts for the Raspberry Pi 4 Model B can be found in Datasheet:
- Name: Raspberry Pi 4 Model B
- Microcontroller: Broadcom BCM2711
- Operating Voltage: 5V (via USB-C)
- Input Voltage: 5V (recommended)
- Number of GPIO Pins: 40
- Digital Pins: 40 (all GPIO pins are digital)
- PWM Pins: 2 PWM channels on 4 GPIO pins (GPIO 12, 13, 18, 19)
- Analog Input Pins: None (requires external ADC for analog input)
- I2C Ports: 1 (two I2C buses available)
- UART Ports: 1 (plus a secondary UART available)
- SPI Ports: 1
- Flash Memory: None (uses microSD card for storage)
- SRAM: 1 GB, 2 GB, or 4 GB RAM options (LPDDR4)
- EEPROM: Not applicable (external storage through microSD)
- Clock Speed: Up to 1.5 GHz (quad-core ARM Cortex-A72)
Comparison as Group Assignment
In this part I have compared the performance and development workflows for Arduino UNO and the Raspberry Pi 4B architecture.
Performance
Processing Power:
- Arduino UNO: Features an 8-bit ATmega328P microcontroller with a clock speed of 16 MHz. It's suitable for simple tasks, real-time applications, and sensor control.
- Raspberry Pi: A single-board computer with various models (e.g., Raspberry Pi 4) that have quad-core ARM processors running at speeds of up to 1.5 GHz. It can handle complex tasks, multitasking, and run a full operating system (Linux).
Memory:
- Arduino UNO: Has 2 KB of SRAM and 32 KB of flash memory, limiting the complexity of applications.
- Raspberry Pi: Comes with RAM options ranging from 1 GB to 8 GB, enabling it to run more complex applications, databases, and web servers.
I/O Capabilities:
- Arduino UNO: Provides 14 digital I/O pins and 6 analog input pins, making it great for direct hardware control.
- Raspberry Pi: Offers GPIO pins but is more suited for higher-level control and interfacing with peripherals through protocols like I2C, SPI, and UART.
Development Workflows
Programming Environment:
- Arduino UNO: Typically programmed using the Arduino IDE, which provides a straightforward interface and a simplified C/C++ programming environment. The development workflow includes writing code, uploading it via USB, and debugging using serial communication.
- Raspberry Pi: Supports multiple programming languages (Python, C/C++, Java, etc.) and can be programmed using various IDEs (Thonny for Python, Geany, etc.). Development can occur directly on the device or remotely via SSH.
Operating System:
- Arduino UNO: Does not run an operating system; it runs a single program at a time directly on the hardware.
- Raspberry Pi: Runs a full operating system (typically Raspberry Pi OS), allowing for multitasking and running background processes.
Libraries and Community Support:
- Arduino UNO: Has a vast collection of libraries for hardware interfacing, with strong community support and numerous tutorials available.
- Raspberry Pi: Also has a large community with extensive libraries for various applications, including multimedia, networking, and IoT projects.
Application Use Cases:
- Arduino UNO: Ideal for embedded systems, robotics, and simple sensor-based projects where real-time performance is critical.
- Raspberry Pi: Suitable for more complex applications such as web servers, media centers, home automation, and machine learning tasks.
In summary, the Arduino UNO is best for straightforward, real-time control tasks with limited resources, while the Raspberry Pi offers higher processing power, memory, and flexibility, making it ideal for more complex applications. The development workflows reflect these differences, with Arduino focusing on embedded programming and Raspberry Pi supporting a full-fledged computing environment.
Try Arduino UNO
The first architecture I tried is Arduino UNO. As it is a easier board to get start with.
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);
}
When the button is pressed,the red light will glow.
When the button is released, the green light will glow.
Servo Control with Ultrasonic Sensor Input
In this exercise, i have tried to use ultrasonic sensor as input to trigger a servo motor to rotate. This interaction happens when an object enter or leave the sensor's detection range.
I have first designed the circuit online then write the code and deployed on it. The code applied as below:#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
In order to compare the performance and development workflows for other architectures, 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.
Communication between both architectures
In this part, I connect an Arduino to a Raspberry Pi and have the Arduino send "Hello from Arduino" to the Raspberry Pi, which will blink an LED upon receiving the command from the Arduino.
Materials Needed:
- Arduino
- Raspberry Pi
- USB cable
- LED
- Resistor
- Breadboard and wires
Steps for communication:
Connect the LED to Pin 11: Connect the longer leg (anode) of the LED to GPIO pin 17 on the Raspberry Pi. Connect the shorter leg (cathode) to a resistor and then to the ground (GND).
Set Up the Raspberry Pi:
Open Python 3 in a new window on the Raspberry Pi. Write and save the following code:
import serial
import RPi.GPIO as GPIO
import time
# Initialize serial communication with Arduino
ser = serial.Serial("/dev/ttyACM0", 9600)
# change ACM number as found from ls /dev/tty/ACM* The ACM number refers to the specific device identifier for a USB-connected serial device on a Unix-like operating system, such as Linux or macOS. When you connect a USB device to your computer, it is assigned a unique identifier like /dev/ttyACM0, /dev/ttyACM1, etc. The number at the end (0, 1, etc.) increments based on the order in which devices are connected.
ser.baudrate = 9600
# Define a function to blink an LED
def blink(pin):
GPIO.output(pin, GPIO.HIGH) # Turn the LED on
time.sleep(1) # Wait for 1 second
GPIO.output(pin, GPIO.LOW) # Turn the LED off
time.sleep(1) # Wait for 1 second
return
# Set up GPIO using Board numbering
GPIO.setmode(GPIO.BOARD)
GPIO.setup(17, GPIO.OUT) # Set pin 17 as an output pin
# Main loop
while True:
# Read a line from the serial input
read_ser = ser.readline().decode('utf-8').strip()
print(read_ser) # Print the received message
# Check if the received message matches "Hello From Arduino!"
if read_ser == "Hello From Arduino!":
blink(17) # Blink the LED connected to pin 17
Set Up the Arduino: Open Arduino IDE and upload the following code to Arduino:
String data = "Hello From Arduino!"; void setup() { Serial.begin(9600); } void loop() { Serial.println(data); // data that is being Sent delay(200); }
Enable Serial and I2C on Raspberry Pi: Open raspi-config and enable both Serial and I2C interfaces.
Install Required Libraries:
sudo apt-get install python-serial sudo pip install pyserial
Connect Arduino to Raspberry Pi: Use a USB cable to connect the Arduino to the Raspberry Pi. Execute ls /dev/tty* in the terminal and find a line like /dev/ttyACM0 or similar.
Update the Python Code: Update the line ser = serial.Serial("/dev/ttyACM1", 9600) to match the ACM number you found, e.g., ser = serial.Serial("/dev/ttyACM0", 9600)
Run the Python Program: Now run the Python program you created. You should see "Hello From Arduino!" in the Python terminal, and the LED should blink!
The result is shown as video below.