UNIT IV OPEN PLATFORMS AND PROGRAMMING
IOT deployment for Raspberry Pi /Arduino Platform-Architecture – Programming – Interfacing –
Accessing GPIO Pins – Sending and Receiving Signals Using GPIO Pins – Connecting to the Cloud.
Arduino Architecture
A typical example of the Arduino board is Arduino Uno. It includes an ATmega328 microcontroller and
it has 28-pins
Arduino Pin Diagram
The Arduino architecture follows a design concept called the Harvard architecture. This means that the
program code and program data are stored in separate memories. Here's a breakdown of the key
components:
Hardware Components:
• Microcontroller: The brains of the Arduino board, typically an Atmel AVR series chip like the
ATmega328P. It reads inputs, executes code, and controls outputs.
• Memory:
o Flash Memory: Stores the Arduino program code (sketch) that you upload. This memory
is non-volatile, meaning the code persists even after power is turned off.
o SRAM (Static Random-Access Memory): Stores temporary data used by the program
while it's running. This memory is volatile, meaning data is lost when power is turned
off.
o EEPROM (Electrically Erasable Programmable Read-Only Memory): Used for
storing semi-permanent data that needs to be retained even after power cycles. Unlike
flash memory, EEPROM has a limited number of write cycles.
• Input/Output (I/O) Pins: These pins allow the Arduino to interact with the external world.
o Digital Pins: Can be set as either input (read data from sensors) or output (control lights,
motors, etc.).
o Analog Pins: Can read analog voltage signals from sensors and convert them to digital
values.
• Power Supply: The Arduino board can be powered via a USB cable from a computer or a
separate DC power supply.
• Voltage Regulator: Regulates the input voltage to ensure a stable 5V supply for the
microcontroller and other components.
• Crystal Oscillator: Provides a precise clock signal that the microcontroller uses to synchronize
its operations.
• Other components: May include LEDs, buttons, voltage references, and communication
interfaces (USB, SPI, I2C) depending on the specific Arduino board model.
Software Components:
• Arduino IDE (Integrated Development Environment): A free software application where you
write, compile, and upload code (sketches) to your Arduino board.
• Arduino Sketch: The code written in a simplified C++ language specifically designed for
Arduino users.
How it Works:
1. You write your code (sketch) in the Arduino IDE.
2. The IDE compiles the code into a machine language format that the microcontroller can
understand.
3. You upload the compiled code to the Arduino board's flash memory via the USB cable.
4. When you power up the Arduino board, the microcontroller fetches instructions from the flash
memory and executes them.
5. The program interacts with the external world through the I/O pins, reading sensor data,
controlling actuators, and communicating with other devices.
Key Points:
• The Harvard architecture keeps program code and data separate, simplifying development.
• The Arduino IDE provides a user-friendly platform for beginners to learn programming and
electronics.
• The modular design of Arduino boards allows for easy expansion using various shields and
components.
Raspberry Pi Architecture
Raspberry Pi is a small single-board computer (SBC). It is a credit card-sized computer that can be
plugged into a monitor. It acts as a minicomputer by connecting the keyboard, mouse, and display.
Raspberry Pi has an ARM processor and 512MB of RAM.
The following diagram shows some main blocks of Raspberry Pi:
Raspberry Pi mainly consists of the following blocks:
• Processor: Raspberry Pi uses Broadcom BCM2835 system on chip which is an ARM
processor and Video core Graphics Processing Unit (GPU). It is the heart of the Raspberry
Pi which controls the operations of all the connected devices and handles all the required
computations.
• HDMI: High Definition Multimedia Interface is used for transmitting video or digital
audio data to a computer monitor or to digital TV. This HDMI port helps Raspberry Pi to
connect its signals to any digital device such as a monitor digital TV or display through an
HDMI cable.
• GPIO ports: General Purpose Input Output ports are available on Raspberry Pi which
allows the user to interface various I/P devices.
• Audio output: An audio connector is available for connecting audio output devices such
as headphones and speakers.
• USB ports: This is a common port available for various peripherals such as a mouse,
keyboard, or any other I/P device. With the help of a USB port, the system can be
expanded by connecting more peripherals.
• SD card: The SD card slot is available on Raspberry Pi. An SD card with an operating
system installed is required for booting the device.
• Ethernet: The ethernet connector allows access to the wired network, it is available only
on the model B of Raspberry Pi.
• Power supply: A micro USB power connector is available onto which a 5V power supply
can be connected.
• Camera module: Camera Serial Interface (CSI) connects the Broadcom processor to the
Pi camera.
• Display: Display Serial Interface (DSI) is used for connecting LCD to Raspberry Pi using
15 15-pin ribbon cables. DSI provides a high-resolution display interface that is
specifically used for sending video data.
Programming Arduino
Programming an Arduino involves writing code in the Arduino Integrated Development Environment
(IDE) and uploading it to the microcontroller on the Arduino board. Here's a step-by-step guide to get you
started with programming an Arduino:
1. Setting Up the Arduino IDE
• Download and Install: Download the Arduino IDE from the official Arduino website and install
it on your computer.
• Connect Your Arduino: Use a USB cable to connect your Arduino board to your computer.
2. Understanding the Arduino IDE
• Sketch: In Arduino terminology, a program is called a sketch. A sketch is written in C/C++.
• Structure of a Sketch:
• Setup Function: void setup() { ... } - runs once at the start.
• Loop Function: void loop() { ... } - runs continuously after setup().
3. Basic Components of a Sketch
• Setup Function: Initializes variables, pin modes, start using libraries, etc.
void setup() {
// Initialization code here
}
• Loop Function: Contains the main code that repeats indefinitely.
void loop() {
// Main code here
}
4. Writing Your First Program
Let's write a simple program to blink an LED on the Arduino board.
Blink LED Example
void setup() {
// Set the built-in LED pin as an output
pinMode(LED_BUILTIN, OUTPUT);
void loop() {
// Turn the LED on
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1 second (1000 milliseconds)
// Turn the LED off
digitalWrite(LED_BUILTIN, LOW);
delay(1000); // Wait for 1 second
5. Uploading the Sketch to the Arduino
• Select the Board: In the Arduino IDE, go to Tools > Board and select your Arduino model.
• Select the Port: Go to Tools > Port and select the port your Arduino is connected to.
• Upload: Click the upload button (right arrow icon) in the IDE to compile and upload the sketch
to the Arduino.
6. Serial Communication
You can use the Serial Monitor to communicate with your Arduino.
• Initialize Serial Communication: In setup(), initialize serial communication with
Serial.begin(9600);.
• Send Data: Use Serial.print() or Serial.println() to send data from the Arduino to the Serial
Monitor.
• Receive Data: Use Serial.read() to read incoming data.
Serial Communication Example
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 baud rate
void loop() {
Serial.println("Hello, world!");
delay(1000); // Print message every second
7. Using Libraries
Arduino libraries extend the functionality of your sketches. You can manage libraries through the
Arduino IDE.
• Include a Library: Use #include <LibraryName.h> at the top of your sketch.
• Install Libraries: Go to Sketch > Include Library > Manage Libraries to add new libraries.
8. Example Projects
• Reading a Sensor: Connect a temperature sensor and read values.
• Controlling a Servo: Use a servo motor and control its position.
• Displaying Data: Use an LCD display to show sensor data.
Reading a Sensor Example (Analog Input)
const int sensorPin = A0; // Analog input pin for the sensor
void setup() {
Serial.begin(9600); // Initialize serial communication
void loop() {
int sensorValue = analogRead(sensorPin); // Read the analog sensor value
Serial.println(sensorValue); // Print the value to the Serial Monitor
delay(500); // Wait for half a second
Tips for Successful Arduino Programming
• Comment Your Code: Use comments (// for single line, /* ... */ for multi-line) to explain your
code.
• Debugging: Use the Serial Monitor to print out values and debug your code.
• Practice: Experiment with different sensors, actuators, and libraries to build your skills.
Interfacing with Arduino
Interfacing an Arduino involves connecting it with various sensors, actuators, and other devices to interact
with the physical world. Here's a comprehensive guide to help you get started with interfacing different
components with your Arduino.
1. Digital Input/Output
Arduino digital pins can be used as input or output.
Digital Input Example: Button
• Components: Arduino, push button, resistor (10kΩ).
• Connection:
• Connect one side of the button to 5V.
• Connect the other side to a digital pin (e.g., pin 2) and ground through a resistor.
const int buttonPin = 2; // Pin where the button is connected
int buttonState = 0; // Variable for reading the button status
void setup() {
pinMode(buttonPin, INPUT); // Set button pin as an input
Serial.begin(9600); // Initialize serial communication
void loop() {
buttonState = digitalRead(buttonPin); // Read the state of the button
Serial.println(buttonState); // Print the button state to the Serial Monitor
delay(100); // Wait a little bit before reading again
Digital Output Example: LED
• Components: Arduino, LED, resistor (220Ω).
• Connection:
• Connect the positive leg (anode) of the LED to a digital pin (e.g., pin 13).
• Connect the negative leg (cathode) to ground through a resistor.
const int ledPin = 13; // Pin where the LED is connected
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as an output
void loop() {
digitalWrite(ledPin, HIGH); // Turn the LED on
delay(1000); // Wait for 1 second
digitalWrite(ledPin, LOW); // Turn the LED off
delay(1000); // Wait for 1 second
2. Analog Input
Arduino analog pins (A0-A5) read values from 0 to 1023, representing 0 to 5V.
Analog Input Example: Potentiometer
• Components: Arduino, potentiometer.
• Connection:
• Connect the two outer pins of the potentiometer to 5V and ground.
• Connect the middle pin to an analog input pin (e.g., A0).
const int potPin = A0; // Pin where the potentiometer is connected
void setup() {
Serial.begin(9600); // Initialize serial communication
void loop() {
int potValue = analogRead(potPin); // Read the potentiometer value
Serial.println(potValue); // Print the value to the Serial Monitor
delay(100); // Wait a little bit before reading again
3. Analog Output (PWM)
Arduino can output PWM signals on digital pins marked with ~.
Analog Output Example: Dimming an LED
• Components: Arduino, LED, resistor (220Ω).
• Connection:
• Connect the LED and resistor in series to a PWM pin (e.g., pin 9).
const int ledPin = 9; // Pin where the LED is connected
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as an output
void loop() {
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(ledPin, brightness); // Set the brightness
delay(10); // Wait for 10 milliseconds
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(ledPin, brightness); // Set the brightness
delay(10); // Wait for 10 milliseconds
4. I2C Communication
I2C allows multiple devices to communicate using two wires: SDA (data) and SCL (clock).
I2C Example: LCD Display
• Components: Arduino, I2C LCD display.
• Connection:
• Connect SDA to A4 and SCL to A5 on the Arduino Uno (other models may vary).
• Connect power and ground.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address and dimensions
void setup() {
lcd.begin(); // Initialize the LCD
lcd.print("Hello, World!"); // Print a message to the LCD
void loop() {
// Nothing to do here
5. SPI Communication
SPI uses three main lines: MOSI, MISO, and SCK, plus a Chip Select (CS) line for each device.
SPI Example: SD Card Module
• Components: Arduino, SD card module.
• Connection:
• Connect MISO, MOSI, SCK, and CS to corresponding Arduino pins (10, 11, 12, 13 on
Uno).
#include <SPI.h>
#include <SD.h>
const int chipSelect = 10; // Chip select pin for the SD card module
void setup() {
Serial.begin(9600);
if (!SD.begin(chipSelect)) {
Serial.println("Initialization failed!");
return;
Serial.println("Initialization done.");
void loop() {
// Nothing to do here
}
6. Interfacing with Sensors
Arduino can interface with a variety of sensors to collect data from the environment.
Example: Temperature Sensor (LM35)
• Components: Arduino, LM35 temperature sensor.
• Connection:
• Connect the sensor's VCC to 5V, GND to ground, and output to an analog pin (e.g., A0).
const int tempPin = A0; // Pin where the temperature sensor is connected
void setup() {
Serial.begin(9600); // Initialize serial communication
void loop() {
int tempValue = analogRead(tempPin); // Read the sensor value
float temperature = (tempValue / 1024.0) * 5.0 * 100; // Convert to
temperature
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
delay(1000); // Wait for 1 second before repeating
Accessing GPIO Pins of Arduino
Accessing GPIO (General Purpose Input/Output) pins on an Arduino is fundamental to interacting with
various hardware components. Here’s a detailed guide on how to access and use the GPIO pins on an
Arduino.
GPIO Pin Overview
On most Arduino boards, such as the Arduino Uno, Nano, and Mega, GPIO pins are divided into:
• Digital Pins: Used for digital input and output.
• Analog Pins: Used for analog input (and sometimes for digital input/output).
1. Digital Pins
Digital pins can read (input) or write (output) HIGH (5V) or LOW (0V) signals.
Setting Up Digital Pins
Digital Output
To set a pin as an output and control an LED:
• Hardware: Connect an LED and a 220Ω resistor in series. Connect the anode to a digital pin
(e.g., pin 13) and the cathode to ground.
const int ledPin = 13; // Pin number for the LED
void setup() {
pinMode(ledPin, OUTPUT); // Set pin as output
void loop() {
digitalWrite(ledPin, HIGH); // Turn LED on
delay(1000); // Wait for 1 second
digitalWrite(ledPin, LOW); // Turn LED off
delay(1000); // Wait for 1 second
Digital Input
To read the state of a button:
• Hardware: Connect a push button. One side to 5V, the other side to a digital pin (e.g., pin 2) and
ground through a 10kΩ resistor.
const int buttonPin = 2; // Pin number for the button
int buttonState = 0; // Variable to hold the button state
void setup() {
pinMode(buttonPin, INPUT); // Set pin as input
Serial.begin(9600); // Initialize serial communication
void loop() {
buttonState = digitalRead(buttonPin); // Read the button state
Serial.println(buttonState); // Print the button state to the Serial Monitor
delay(100); // Wait for a short period before reading again
2. Analog Pins
Analog pins are primarily used for reading analog signals but can also be used as digital pins.
Reading Analog Values
Analog pins read values between 0 and 1023, representing 0V to 5V.
Example: Reading a Potentiometer
• Hardware: Connect a potentiometer. The middle pin to an analog input (e.g., A0), and the other
two pins to 5V and ground.
const int potPin = A0; // Pin number for the potentiometer
void setup() {
Serial.begin(9600); // Initialize serial communication
void loop() {
int potValue = analogRead(potPin); // Read the potentiometer value
Serial.println(potValue); // Print the value to the Serial Monitor
delay(100); // Wait for a short period before reading again
3. Using Analog Pins as Digital Pins
Analog pins can be used as digital pins, referenced by their pin numbers. For example, on an Arduino
Uno:
• A0 as digital pin 14
• A1 as digital pin 15
• And so on.
const int analogPinAsDigital = 14; // A0 as digital pin 14
void setup() {
pinMode(analogPinAsDigital, OUTPUT); // Set pin as output
}
void loop() {
digitalWrite(analogPinAsDigital, HIGH); // Set the pin HIGH
delay(1000); // Wait for 1 second
digitalWrite(analogPinAsDigital, LOW); // Set the pin LOW
delay(1000); // Wait for 1 second
Sending and Receiving Signals Using GPIO Pins
Sending and receiving signals using GPIO pins on an Arduino involves configuring pins as either inputs
or outputs and using appropriate functions to read from or write to these pins. This capability allows the
Arduino to interact with a variety of components, including sensors, actuators, and other microcontrollers.
Here's a detailed guide on how to send and receive digital and analog signals using GPIO pins on an
Arduino.
Digital Signals
Digital Output: Controlling an LED
To control an LED, configure a GPIO pin as an output and use the digitalWrite() function.
Circuit:
• Connect the anode (longer leg) of the LED to a digital pin (e.g., pin 13) through a 220Ω resistor.
• Connect the cathode (shorter leg) to ground.
Code:
const int ledPin = 13; // Pin connected to the LED
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
void loop() {
digitalWrite(ledPin, HIGH); // Turn the LED on
delay(1000); // Wait for 1 second
digitalWrite(ledPin, LOW); // Turn the LED off
delay(1000); // Wait for 1 second
}
Digital Input: Reading a Button State
To read a button state, configure a GPIO pin as an input and use the digitalRead() function.
Circuit:
• Connect one terminal of the push button to 5V.
• Connect the other terminal to a digital pin (e.g., pin 2) and to ground through a 10kΩ resistor
(pull-down resistor).
Code:
const int buttonPin = 2; // Pin connected to the button
int buttonState = 0; // Variable to store the button state
void setup() {
pinMode(buttonPin, INPUT); // Set the button pin as input
Serial.begin(9600); // Initialize serial communication
void loop() {
buttonState = digitalRead(buttonPin); // Read the button state
Serial.println(buttonState); // Print the button state
delay(100); // Wait for a short period
Analog Signals
Analog Input: Reading a Potentiometer
To read an analog signal, use the analogRead() function on an analog pin (A0-A5).
Circuit:
• Connect the middle pin of the potentiometer to an analog input pin (e.g., A0).
• Connect the other two pins to 5V and ground.
Code:
const int potPin = A0; // Pin connected to the potentiometer
void setup() {
Serial.begin(9600); // Initialize serial communication
void loop() {
int potValue = analogRead(potPin); // Read the potentiometer value
Serial.println(potValue); // Print the potentiometer value
delay(100); // Wait for a short period
Analog Output: Controlling LED Brightness with PWM
To simulate an analog output using PWM, use the analogWrite() function on a PWM-capable pin
(marked with ~).
Circuit:
• Connect the anode of the LED to a PWM-capable pin (e.g., pin 9) through a 220Ω resistor.
• Connect the cathode to ground.
Code:
const int ledPin = 9; // Pin connected to the LED
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
void loop() {
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(ledPin, brightness); // Set the LED brightness
delay(10); // Wait for 10 milliseconds
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(ledPin, brightness); // Set the LED brightness
delay(10); // Wait for 10 milliseconds
Sending and Receiving Digital Signals between Two Arduinos
To communicate between two Arduinos using digital signals, connect a digital output pin from one
Arduino to a digital input pin on the other.
Circuit:
• Arduino A (Sender):
• Connect a digital pin (e.g., pin 7) to the digital input pin of Arduino B (e.g., pin 8).
• Arduino B (Receiver):
• Connect the ground pins of both Arduinos together.
Code for Arduino A (Sender):
const int outPin = 7; // Pin to send signal
void setup() {
pinMode(outPin, OUTPUT); // Set the pin as output
void loop() {
digitalWrite(outPin, HIGH); // Send HIGH signal
delay(1000); // Wait for 1 second
digitalWrite(outPin, LOW); // Send LOW signal
delay(1000); // Wait for 1 second
Code for Arduino B (Receiver):
const int inPin = 8; // Pin to receive signal
int signalState = 0; // Variable to store the received signal state
void setup() {
pinMode(inPin, INPUT); // Set the pin as input
Serial.begin(9600); // Initialize serial communication
void loop() {
signalState = digitalRead(inPin); // Read the signal state
Serial.println(signalState); // Print the received signal state
delay(100); // Wait for a short period
Connecting an arduino to the Cloud
Connecting an Arduino to the cloud allows you to send data from your Arduino to online services, receive
commands from the cloud, and create Internet of Things (IoT) applications. Here’s a guide on how to
connect your Arduino to the cloud using various methods:
1. Using Wi-Fi with Arduino
Components Needed:
• Arduino board (e.g., Arduino Uno, Arduino Nano)
• Wi-Fi module (e.g., ESP8266, ESP32)
Using ESP8266 Wi-Fi Module
The ESP8266 can be used to connect your Arduino to a Wi-Fi network and then to the cloud.
Circuit:
• Connect the ESP8266 module to the Arduino as follows:
• VCC to 3.3V
• GND to Ground
• TX to Arduino RX (Pin 0)
• RX to Arduino TX (Pin 1)
• CH_PD to 3.3V
• GPIO0 to 3.3V
2. Using Arduino IoT Cloud
Arduino IoT Cloud is an official service from Arduino for connecting devices to the cloud.
Steps to Set Up:
1. Create an Arduino Account: Sign up for an account at Arduino IoT Cloud.
2. Set Up a Thing:
• Create a new Thing on the Arduino IoT Cloud dashboard.
• Add properties to your Thing (e.g., temperature, humidity).
3. Configure Network and Device:
• Set up your device and network credentials in the Thing configuration.
4. Install Libraries:
• Install the ArduinoIoTCloud and WiFiNINA libraries from the Arduino Library Manager.
5. Write Code:
• Use the generated sketch to connect your Arduino to the cloud.
3. Using MQTT for Cloud Communication
MQTT is a lightweight messaging protocol for small sensors and mobile devices, making it ideal for IoT
applications.
Components Needed:
• Arduino board
• Ethernet shield or Wi-Fi module
• MQTT broker (e.g., Mosquitto, Adafruit IO)
Circuit:
• Connect the Arduino to the network via Ethernet or Wi-Fi.