0% found this document useful (0 votes)
13 views27 pages

Chapter 3InterfacingSensorandactuators

The Arduino Uno is a beginner-friendly microcontroller board that connects to a computer via USB for power and communication. It features various pins for power supply, analog and digital input/output, PWM, and serial communication, allowing it to interface with sensors and actuators. The document also provides examples of programming for controlling LEDs and traffic lights using the Arduino Uno, demonstrating its capabilities in practical applications.

Uploaded by

Farhaan Kammar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views27 pages

Chapter 3InterfacingSensorandactuators

The Arduino Uno is a beginner-friendly microcontroller board that connects to a computer via USB for power and communication. It features various pins for power supply, analog and digital input/output, PWM, and serial communication, allowing it to interface with sensors and actuators. The document also provides examples of programming for controlling LEDs and traffic lights using the Arduino Uno, demonstrating its capabilities in practical applications.

Uploaded by

Farhaan Kammar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Arduino Uno

As we discussed we know that Arduino Uno is the most standard board available and probably
the best choice for a beginner. We can directly connect the board to the computer via a USB
Cable which performs the function of supplying the power as well as acting as a serial port.

Image Source: [Link]

Vin: This is the input voltage pin of the Arduino board used to provide input supply from an
external power source.

5V: This pin of the Arduino board is used as a regulated power supply voltage and it is used to
give supply to the board as well as onboard components.

3.3V: This pin of the board is used to provide a supply of 3.3V which is generated from a
voltage regulator on the board
GND: This pin of the board is used to ground the Arduino board.

Reset: This pin of the board is used to reset the microcontroller. It is used to Resets the
microcontroller.

Analog Pins: The pins A0 to A5 are used as an analog input and it is in the range of 0-5V.

Digital Pins: The pins 0 to 13 are used as a digital input or output for the Arduino board.

Serial Pins: These pins are also known as a UART pin. It is used for communication between
the Arduino board and a computer or other devices. The transmitter pin number 1 and receiver
pin number 0 is used to transmit and receive the data resp.

External Interrupt Pins: This pin of the Arduino board is used to produce the External interrupt
and it is done by pin numbers 2 and 3.

PWM Pins: This pins of the board is used to convert the digital signal into an analog by varying
the width of the Pulse. The pin numbers 3,5,6,9,10 and 11 are used as a PWM pin.

SPI Pins: This is the Serial Peripheral Interface pin, it is used to maintain SPI
communication with the help of the SPI library. SPI pins include:

1. SS: Pin number 10 is used as a Slave Select


2. MOSI: Pin number 11 is used as a Master Out Slave In
3. MISO: Pin number 12 is used as a Master In Slave Out
4. SCK: Pin number 13 is used as a Serial Clock

LED Pin: The board has an inbuilt LED using digital pin-13. The LED glows only when the
digital pin becomes high.

AREF Pin: This is an analog reference pin of the Arduino board. It is used to provide a
reference voltage from an external power supply.

Arduino boards provide different types of interfaces that allow them to connect with sensors, actuators,
computers, and other devices. These interfaces are mainly grouped into communication interfaces,
input/output interfaces, and power interfaces.

1. Digital Input/Output (I/O) Interface

read (input) or send (output) digital signals (HIGH/LOW).


Example uses:

 LED ON/OFF
 Reading button press

On Arduino Uno: Pins: 0–13


pinMode(), digitalWrite(), digitalRead()

2) Analog Input Interface


varying voltage (0–5V) from sensors.

 Temperature sensor
 Light sensor (LDR)

A0–A5
analogRead()

3) PWM (Pulse Width Modulation) Interface

 LED brightness control


 Motor speed control

ins (Arduino Uno):3, 5, 6, 9, 10, 11


analogWrite()
4) Serial Communication (UART)

 Computer
 Other microcontrollers

TX (1), RX (0)

[Link](), [Link]()

5)I2C (Inter-Integrated Circuit)


2 wires only:

 SDA (data)
 SCL (clock)

multiple devices on same bus

 SDA → A4
 SCL → A5
Wire.h

6) SPI (Serial Peripheral Interface)

 MOSI → 11
 MISO → 12
 SCK → 13
 SS → 10

 SD cards
 Displays

7) Power Interface
power to board and components
5V, 3.3V, GND, VIN

 USB cable
 External adapter (7–12V)

EXAMPLES

1) LED Blink Program


void setup() {

pinMode(13, OUTPUT);

void loop() {

digitalWrite(13, HIGH); // LED ON

delay(1000);

digitalWrite(13, LOW); // LED OFF

delay(1000);

2) Working with Servo Motor


The code to run the Servo Motor is as shown below:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup()
{
[Link](3); // attaches the servo on pin 3 to the servo object
}
void loop()
{
for(pos = 0; pos ¡= 180; pos += 1) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
[Link](pos); //tell servo to go to position in variable ’pos’
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos¿=0; pos-=1) // goes from 180 degrees to 0 degrees
{
[Link](pos); // tell servo to go to position in variable ’pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
3) Programming for Arduino controlled traffic-control lights (TLs) at
a road junction:
Assume Arduino Uno board as an embedded device platform
(Section 8.4.1) for the following project:
three TLs—Red, Yellow and Green needs to be controlled on each of the four north, east, south
and west
clockwise pathways.
Let twelve GPIO pins on Uno connect twelve number externally connected LEDs, R0, Y0, G0,
R1, Y1, G1, R2,
Y2, G2, R3, Y3, and G3, (four sets of three R, G, Y LEDs each). The port LEDs represents the
TLs during the
prototype-development and testing-stage.
How can the port LEDs be On-Off programmed so that north and south pathways directed roads
and traffic
is switched on and east and west pathways traffic switched off?
Solution
First step is declaring the data types, constants, variables and functions used. Second and third
steps are
coding for setup() and loop(). Example of program codes, which embeds onto Uno are as follows:
/*Assume twelve digital IO ports assigned to external LEDs are port 2 to 12 and
port 14. The ports are programmed according to TLs switching ON or OFF./
/* Pin 13 connects the board LED, and be used for indicating successful running
of the developed codes during testing phases.*/
int internalLED = 13;
/* Variables are written using a lower case first character */
int ledR0, ledY0, ledG0, ledR1, ledY1, ledG1, ledR2, ledY2, ledG2, ledR3,
ledY3, ledG3;
Assign the pins to the respectively connected LEDs */
ledR0 = 2; ledY0 = 3; ledG4 = 4; ledR1 = 5; ledY1 = 6;ledG1 = 7; ledR2 = 8;
ledY2 = 9; ledG2 = 10; ledR3 = 11; ledY3 = 12; ledG3 = 14;
/* Declare Functions for sequences of traffic lights ON-OFF as follows: */
void north_south_Green() {
digitalWrite (ledR0, LOW); digitalWrite (ledY0, LOW); digitalWrite (ledG0,
HIGH);
digitalWrite (ledR2, LOW); digitalWrite (ledY2, LOW); digitalWrite (ledG2,
HIGH);
};
/* Function Switch RED ON for East and West pathways*/
void east_west_Red() {
digitalWrite (ledR1, HIGH); digitalWrite (ledY1, LOW);digitalWrite (ledG1,
LOW);
digitalWrite (ledR3, HIGH); digitalWrite (ledY3, LOW); digitalWrite (ledG3,
LOW);
};
/**********************************************************/
void setup ( ) {
/* GPIO pins 2 to 12 and 14 are thus assigned port numbers corresponding to 12
external LEDs, R0, Y0, G0, R1, Y1, G1, R2, Y2, G2, R3, Y3, and G3.*/
/* Assign mode of each pin as output */
pinmode (ledR0, OUTPUT); // Constants are written in Upper Cases
pinmode (ledY0, OUTPUT);
.
pinmode (ledY3, OUTPUT);
pinmode (ledG3, OUTPUT);
/* Let Pin 13 be used for indicating successful running of the developed codes
during testing phases. Initialise internal Port 13 Digital IO Pin LED for
test.*/
pinmode (internalLED, OUTPUT);
/* Initialise start of the Board and Sequences. */
digitalWrite (internalLED, HIGH);
/* Display the settings of Digital IO pins at serial display-monitor on the
computer where IDE is setup. */
/*Let UART mode baud rate = 9600*/
[Link] (9600);

[Link] (“Arduino [Link] for controlling three traffic signals-


Red, Yellow and Green at four pathways”);

[Link] (“Arudino board LED glows when cycle starts for the sequences of
lights turning high and turns off for brief interval in order to indicate the
successful completion of the cycle”);
[Link] (“Twelve 12 external LEDs, R0, Y0, G0, R1, Y1, G1, R2, Y2, G2,
R3, Y3, and G3 corresponds to 12 traffic lights at north, east, south, west
four pathways”)
}
/***********************************************************************/
Step: Loop function which endlessly runs
void loop () {
/* Assume no right turn from pathways or left turn from pathways permitted, just
for simplicity and for learning the basics. */
/*Switch Green ON for North and South pathways */
/*Switch RED ON for East and West pathways*/
// Run Functions
north_south_Green();
east_west_Red ();
}
4) Programming for Arduino controlled traffic-control lights
(TLs) at a road junction with control of on intervals:
Assume the same set up and LEDs with the same connection as described in Example 9.1.
Assume that setup
remains the same as before. Therefore, the steps 1 and 2 of Example 9.1 are same in the present
example
as well.
How can the port LEDs be on-off programmed codes for Step 3: Function loop ( ). The new
codes are for
signalling and lights control of all four pathways. Assume delays of 10 s each between successive
states of
LEDs and steady state for 30 s for a pair of pathways.
Write the Function test ( ) to find that the software is functioning as programmed on the board.
Assume
that test function uses on-board LED at GPIO pin 13 during the period of running of codes.
Solution
Program codes in Step 1 for pre-processing declarations, which embeds onto Uno, are same as in
Example
9.1.
/* Assume twelve digital IO ports assigned to external LEDs are port 2 to 12
.
ledY2 = 9; ledG2 = 10; ledR3 = 11; ledY3 = 12; ledG3 = 14;
/* Declare Functions for sequences of traffic lights ON-OFF as follows: */
void north_south_Green() {
digitalWrite (ledR0, LOW); digitalWrite (ledY0, LOW); digitalWrite (ledG0,
HIGH);
digitalWrite (ledR2, LOW); digitalWrite (ledY2, LOW); digitalWrite (ledG2,
HIGH);
};
/* Function Switch RED ON for East and West pathways*/
void east_west_Red() {
digitalWrite (ledR1, HIGH); digitalWrite (ledY1, LOW);digitalWrite (ledG1,
LOW);
digitalWrite (ledR3, HIGH); digitalWrite (ledY3, LOW); digitalWrite (ledG3,
LOW);
};
/* Function Switch Yellow ON for North and South pathways forbrief interval
before change of state */
void north_south_Yellow (){
digitalWrite (ledR0, LOW); digitalWrite (ledY0 HIGH); digitalWrite (ledG0,
LOW);
digitalWrite (ledR2, LOW); digitalWrite (ledY2, HIGH); digitalWrite (ledG2,
LOW);
};
/* Function Switch Green ON for East and West pathways*/
void east_west_Green (){
digitalWrite (ledR1, LOW); digitalWrite (ledY1, LOW);digitalWrite (ledG1, HIGH);
digitalWrite (ledR3, LOW); digitalWrite (ledY3, LOW);digitalWrite (ledG3, HIGH);
};
/* Function Switch RED ON for North and South pathways*/
void north_south_Red (){
digitalWrite (ledR0, HIGH); digitalWrite (ledY0, LOW); digitalWrite (ledG0,
LOW);
digitalWrite (ledR2, HIGH); digitalWrite (ledY2, LOW);digitalWrite (ledG2,
LOW);
};
/* Function Switch Yellow ON for East and West pathways */
void east_west_Yellow () {
digitalWrite (ledR1, LOW); digitalWrite (ledY1, HIGH);digitalWrite (ledG1,
LOW);
digitalWrite (ledR3, LOW); digitalWrite (ledY3, HIGH); digitalWrite (ledG3,
LOW);
};
/******************************************************************/
Program codes in Step 2 for setup() same as in Example 9.1 as setting is unchanged.
void setup ( ) {
/* GPIO pins 2 to 12 and 14 are thus assigned port numbers corresponding */
.
[Link] (“ Twelve 12 external LEDs, R0, Y0, G0, R1, Y1, G1, R2, Y2, G2,
R3, Y3, and G3 corresponds to 12 traffic lights at north, east, south, west
four pathways”)
}
/****************************************************************/
Modification in Step 3 function loop() are as follows:
void loop () {
/*Assume no right turn from pathways or left turn from pathways permitted, just
for simplicity and for learning the basics*/
/*Function Switch Green ON for North and South pathways */
north_south_Green();
/*Function Switch RED ON for East and West pathways*/
east_west_Red();
//Function delay (30000)
delay (30000); //Wait for 30 secomd
/*Function Switch Yellow ON for North and South pathways forbrief interval
before change of state */
north_south_Yellow ();
//Function delay (10000);
delay (10000); //Wait for 10 secomd
/*Function Switch Green ON for East and West pathways*/
east_west_Green ();
/*Function Switch RED ON for North and South pathways*/
north_south_Red ();
//Function delay (30000);
delay (30000); //Wait for 30 secomd
/*Function Switch Yellow ON for East and West pathways */
east_west_Yellow ();
//Function delay (10000);
delay (10000); //Wait for 10 secomd
/*Testing to show completion of one cycle of traffic light
Let internal board LED flash OFF for 6s before the sequences of lights repeat */
//Statements for test;
digitalWrite (internalLED, LOW); delay (6000); // Wair 6 s
[Link] (“ One Sequence completed” );
digitalWrite (internalLED, HIGH);
}
5) Modification in Step 3 function loop() are as follows:
void loop () {
/*Assume no right turn from pathways or left turn from pathways permitted, just
for simplicity and for learning the basics*/
/*Function Switch Green ON for North and South pathways */
north_south_Green();
/*Function Switch RED ON for East and West pathways*/
east_west_Red();
//Function delay (30000)
delay (30000); //Wait for 30 secomd
/*Function Switch Yellow ON for North and South pathways forbrief interval
before change of state */
north_south_Yellow ();
//Function delay (10000);
delay (10000); //Wait for 10 secomd
/*Function Switch Green ON for East and West pathways*/
east_west_Green ();
/*Function Switch RED ON for North and South pathways*/
north_south_Red ();
//Function delay (30000);
delay (30000); //Wait for 30 secomd
/*Function Switch Yellow ON for East and West pathways */
east_west_Yellow ();
//Function delay (10000);
delay (10000); //Wait for 10 secomd
/*Testing to show completion of one cycle of traffic light
Let internal board LED flash OFF for 6s before the sequences of lights repeat */
//Statements for test;
digitalWrite (internalLED, LOW); delay (6000); // Wair 6 s
[Link] (“ One Sequence completed” );
digitalWrite (internalLED, HIGH);
}
#include <util.h> /*IO utility functions. Includes UART interface, which
connects to computer for display of messages on computer-screen. IDE software
provides the functions for display using serial interface output of board. */
/* Sensed parameter, say, temperature Sensor Input at A0 pin. Define initial
input = 0. */
#define TempSensorADCinput 0
/*calibCoeff = output change in parameter per unit rise in temerature by 1
degree Celsius. For example, calibCoeff = temperature change per unit change
in converted digital value of Vinput from the Sensor.*/
/* Define calibCoeff = 0.097752. */
#define calibCoeff 0.097752 /*The calibCoeff is 100/1023 = 0.097752 when sensed
parameter analog value is between 0 degree Celsius to 100 degree Celsius and
observed digital value is from 0 to 1023 from 10-bit ADC*/
float observedV, parameter; /*observedV = Vinput from Sensor. The parameter =
sensed parameter value 0....1023*/
/*Let internal LED at digital IO Pin 13 be used for indicating successful
measurement and wait for next reading */.
int internalLED = 13; /* initialise internal Port 13 Digital IO Pin LED for
test Function. */
/* Name the unit, whether degree Celsius or RH% */
char [ ] unit;
unit = “degree Celsius”; /* Declare the unit of sensed parameter unit*/
/*****************************************************************/
Step 2 is as follows for setup() statements of the program for setting the board and parameters for
measurement of sensed V.
void setup() {
/* Declare the unit of sensed parameter initial value and initial observed V*/
parameter = 0.0; // Declare the initial value of the parameter
observedV= 0.0; // Declare the observedV = 0 at minimum
[Link] (9600); //Let UART mode baud rate = 9600.
[Link] (“Arduino Program for input for ADC input at the board”);
[Link] (“Check that Arduino pin A0 connects ADC input from the sensor
”);
[Link] (“Check that Arduino 3.3 V pin connects ADC reference pin at the
sensor”);
pinMode (internalLED, OUTPUT);
// Indicate start of the Board and Sequences
digitalWrite (internalLED, HIGH);
// Display the results at serial display monitor on computer where IDE is setup
[Link] (“ Arudino board LED glowing starts when hourly cycle starts
for the sequences of LED lights turning high and turns off for successive 3s
intervals in order to indicate that system is waiting for the next hourly
reading”);

342 Internet of Things: Architecture and Design Principles


}
/*******************************************************************/
Step 3 is as follows for loop() statements of the program for measuring the ADC input and
parameters for
the measurement of sensed V at successive interval.
void loop () {
// ADC uses analog reference voltage and it is internally set.
//A reference voltage is 3.3V at the Uno.
// The value is used to convert the acquired value into appropriate reading,
// for example, temperature value in degree Celsius units or into RH%
//10-bit ADC means analog voltage resolution is (Vref/ 1024)
observedV = analogRead (TempSensorADCinput);
//parameter is found from the calibration coefficient
parameter = calibCoeff*obesrvedV *1023/3.3;
[Link] (“Temperature =”); //Assume sensed parameter is temperature
[Link] (parameter, unit);
test ( );
}
/*****************************************************************/
Step 4 includes a test(). The statements for test function are as follows:
void test ( ){ //Start blinks every three second
/* One Hour wait for next reading. Run the loop 600 times. Each wait for 3+ 3
second. 6 s × 600 = 1 hour and LED blinks at 3s ON-OFF intervals*/
digitalWrite (internalLED, HIGH);}
for ( int i = 1, i<=600) , i++) {
delay (3000);
digitalWrite (internalLED, LOW);
delay (3000); // Wair 6 s
digitalWrite (internalLED, HIGH);
}
Raspberry Pi
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 architecture of
Raspberry Pi is discussed in this article.

The following diagram shows the architecture of Raspberry Pi:

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.

Interfaces of Raspberry Pi Boards

The Raspberry Pi provides a wide range of interfaces to connect sensors, displays, storage
devices, and networks. These interfaces are more advanced compared to microcontrollers like
Arduino because Raspberry Pi is a full computer.

1. GPIO (General Purpose Input/Output)


 Used to connect: LEDs, buttons, sensors
 Digital pins (HIGH/LOW)
 Typical: 40 pins (in models like Raspberry Pi 3/4)
 Controlled using Python ([Link], gpiozero)

2) SPI (Serial Peripheral Interface)


 High-speed communication
 Pins: MOSI, MISO, SCLK, CE
 Used for:

 ADCs (since Pi has no built-in analog input)


 Displays

3) I2C Interface
 Uses only 2 wires:

 SDA (data), SCL (clock)

 Supports multiple devices


 Common uses: Sensors, RTC modules, LCDs
4) UART (Serial Communication)
 Used for:

 Debugging
 Communication with microcontrollers (like Arduino)

 Pins: TXD, RXD


5)USB Interface
 Connect external devices:

 Keyboard, mouse
 USB drives
 Camera modules (via USB)

 Versions: USB 2.0 / USB 3.0 (depending on model)


6)HDMI (Display Interface)
 Used to connect: Monitor or TV
 Supports:Audio + Video output
 Newer models: Micro-HDMI (dual display support)
7)Camera Interface (CSI)
 CSI = Camera Serial Interface

 Used for: Pi Camera module


 Applications:

 Surveillance
 Computer vision

8) Power Interface

 Power input:

 Micro-USB / USB-C (model dependent)

 Typical requirement: 5V, 2.5A–3A

EXAMPLES

1)LED Blink Program


import [Link] as GPIO

import time

LED = 18 # GPIO pin

[Link]([Link])

[Link](LED, [Link])

while True:

[Link](LED, [Link])

[Link](1)

[Link](LED, [Link])

[Link](1)

2) Push Button Input Program


import [Link] as GPIO

BUTTON = 17

[Link]([Link])
[Link](BUTTON, [Link], pull_up_down=GPIO.PUD_UP)

while True:
if [Link](BUTTON) == 0:
print("Button Pressed")

3) PWM (LED Brightness Control)

import [Link] as GPIO


import time

LED = 18

[Link]([Link])
[Link](LED, [Link])

pwm = [Link](LED, 1000) # 1kHz frequency


[Link](0)

for duty in range(0, 101, 5):


[Link](duty)
[Link](0.1)

[Link]()
[Link]()
4) Buzzer Sound Program
import [Link] as GPIO
import time

BUZZER = 23

[Link]([Link])
[Link](BUZZER, [Link])

while True:
[Link](BUZZER, [Link])
[Link](0.5)
[Link](BUZZER, [Link])
[Link](0.5)
5)Temperature Sensor (DHT11)
import Adafruit_DHT

sensor = Adafruit_DHT.DHT11
pin = 4

humidity, temperature = Adafruit_DHT.read(sensor, pin)

if humidity is not None and temperature is not None:


print("Temp = {:.1f}°C Humidity = {}%".format(temperature,
humidity))
else:
print("Sensor failure")
6)Ultrasonic Distance Sensor (HC-SR04)
import [Link] as GPIO
import time

TRIG = 23
ECHO = 24

[Link]([Link])
[Link](TRIG, [Link])
[Link](ECHO, [Link])

[Link](TRIG, False)
[Link](2)
[Link](TRIG, True)
[Link](0.00001)
[Link](TRIG, False)

while [Link](ECHO) == 0:
start = [Link]()

while [Link](ECHO) == 1:
stop = [Link]()

distance = (stop - start) * 17150


print("Distance:", distance, "cm")

[Link]()

You might also like