KEMBAR78
Lecture 7. Aruino Programming Lab | PDF | Arduino | Sensor
0% found this document useful (0 votes)
3 views21 pages

Lecture 7. Aruino Programming Lab

Lecture 7. Arduino programming Lab

Uploaded by

Phial
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)
3 views21 pages

Lecture 7. Aruino Programming Lab

Lecture 7. Arduino programming Lab

Uploaded by

Phial
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
You are on page 1/ 21

UNIVERSITY OF LAY ADVENTIST OF KIGALI (UNILAK)

Nyanza Campus
FACULTY
COMPUTING AND INFORMATION SCIENCES
DEPARTMENT
SOFTWARE ENGINEERING
COURSE: INTERNET OF THINGS
Lecturer: Cecile MUSABYEMARIYA

ARDUINO PROGRAMMING LAB


Programming the Arduino

Step 1: Download the Arduino IDE from


https://www.arduino.cc/en/Main/Software .

Step 2: Connect the Arduino and the PC with the USB


cable that comes with the Arduino.

Step 3: Go to Device Manager, see ports [Windows 10] or


Other Devices [ Other versions of Windows] and check
whether the Arduino device is detected and the drivers are
installed. Also identify the COM Port to which your Arduino
is connected.
Step 4: Open the Arduino IDE and go to Tools -> Port and
select the COM port to which your arduino is connected
with the PC.

Step 5: Go to Tools -> Board -> and select the board that you have
connected with the PC. In this case it is “Arduino / Genuino Uno“.

Step 6: Write your code into the editor of the IDE


Step 7: Click on the tick mark on the top left corner of the
IDE to verify that your code has no compilation errors.
Step 8: Click on the arrow mark next to the tick mark to
upload your code into the board. Look for the progress bar
on the bottom right corner of the IDE and look for the “Done
Uploading” message at the bottom left. Once this message
comes, your code is safely uploaded into the Arduino board.
Step 9: In case, your sketch uses a Serial Monitor for
displaying the output , click on the magnifying glass on the
right top corner of the IDE to open the serial monitor. Select
the baud rate that you have set to in the sketch.
PROGRAM STRUCTURE OF ARDUINO

A basic Arduino sketch consists of two functions called setup() and


loop().
Open the Arduino IDE and select File → Examples → 01.Basics
→ BareMinimum to see the two functions without any definition
for the same.

Global declarations are the variables that are accessible by all the
functions. Generally all the pins are given appropriate names so
that they can be identified easily through the code. It is generally
a good practice to do so.

The setup() function consists of the code that runs only once and
thus is used to set up the board in the desired manner, for
example, setting a particular pin as output, setting the baud rate
for serial communication.

The loop() function is the main firmware that you write into the
Arduino Board. The logic inside this runs forever as long as the
board is powered up. Since embedded systems usually have only
one job that is done repetitively, this is the way to go.
EXPERIMENT ONE [LED BLINK]

LEDs are small, powerful lights that are used in many different
applications. To start, we will work on blinking an LED, the Hello World
of microcontrollers. It is as simple as turning a light on and off.
Establishing this important baseline will give you a solid foundation as
we work towards experiments that are more complex.

Components Required

You will need the following components −


 1 × Breadboard
 1 × Arduino Uno R3
 1 × LED
 1 × 330Ω Resistor
 2 × Jumper

Procedure

Follow the circuit diagram and hook up the components on the


breadboard as shown in the image given below.

Note − To find out the polarity of an LED, look at it closely. The shorter
of the two legs, towards the flat edge of the bulb indicates the negative
terminal.
Components like resistors need to have their terminals bent into 90°
angles in order to fit the breadboard sockets properly. You can also cut
the terminals shorter.

Sketch

Open the Arduino IDE software on your computer. Coding in the


Arduino language will control your circuit. Open the new sketch File
by clicking New.

Arduino Code

/*
Blink
Turns on an LED on for one second, then off for one second,
repeatedly.
*/

// the setup function runs once when you press reset or power the
board

void setup() { // initialize digital pin 13 as an output.


pinMode(2, OUTPUT);
}
// the loop function runs over and over again forever

void loop() {
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
EXPERIMENT TWO [Temperature and Humidity CONTROLLED
LED]

DHT22 Digital Temperature and Humidity Sensor


Monitoring of Environmental parameters is necessary in so many
ways. Not, just for different industries but also for common people.
Both Higher and lower values of different environmental parameters
are not sufficient for a normal being. Hence, there are different
sensors that can help to get the readings of those parameters,
including temperature and humidity. DHT22 is one of those sensors
that get employed for temperature readings.

An Overview about DHT22 Temperature Sensor

DHT22 is more like the DHT11 sensor, but it has a richer measuring
range and somewhat better accuracy. It uses exclusive digital-signal-
collecting-technique and humidity sensing technology with greater
reliability and stability. If you are looking to measure the temperature
from the range of 40°C to 80°C, or want to estimate humidity from
0% to 100%, then this sensor might be a splendid choice for you.
Internal Structure and working Principle

The sensor comprises an NTC temperature sensor, i.e. thermistor, a


humidity sensing component, and an IC on the backside of the
sensor. A thermistor is basically a variable resistor (potentiometer)
that changes its resistance with the change of temperature. And, to
measure the humidity, there is a sensing element that has two
electrodes with moisture-holding substrates between them. When
humidity changes the resistance between these two electrodes gets
changed.

Features and Specifications of DHT22 Sensor

Features

 Full range temperature compensated


 Relative humidity and temperature measurement
 Calibrated digital signal
 Outstanding long-term stability
 Extra components not needed
 Long transmission distance
 ow power consumption
4 pins packaged and fully interchangeable

Specifications

Operating Voltage: 3.5V to 5.5V


 Operating current: 0.3mA (measuring) 60uA (standby)
 Output: Serial data
 Temperature Range: -40°C to 80°C
 Humidity Range: 0% to 100%
 Resolution: Temperature and Humidity both are 16 bit
 Accuracy: ±0.5°C and ±1%
Pinouts DHT22

Pin Pin
Description
number Name

The power supply pin requires


1 Vcc
voltage from 3.5V to 5.5V

Output pin for both humidity and


2 Data
temperature through serial Data
No connection Pin. This pin is not
3 NC
for connecting.

Connection of sensor to the


4 Ground
system’s ground

Applications of DHT11 Sensor

Weather Station

Because of the low cost of this temperature sensor, this can be used
in cheap yet effective weather stations that can depict the
environmental temperature and humidity which may help in various
industries.

Climate Control

Climate control is a system for monitoring the temperature and other


parameters. Therefore, the DHT22 temperature and humidity sensor
may help to get the environmental readings to work accordingly.

Environmental Monitoring

An environmental monitoring system that monitors or controls the


quality of the environment. Hence, this system also employs
humidity and temperature sensors like DHT22.

DHT22 and LED circuit diagram


Arduino code
//Libraries
#include "DHT.h"

//Define Sensor Pins and Type


#define DHTPIN 12 // what pin we're connected to
//Choose Sensor Type
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal
16mhz Arduino

//Variables
int chk;
float hum; //Stores humidity value
float temp; //Stores temperature value
const int HigherTemp = 26; //The higher temperature we want to
trigger the red light

//Define LED pins


const int LED1_Green = 8;
const int LED2_Red = 9;

void setup() {
Serial.begin(9600);
dht.begin();

pinMode(LED1_Green, OUTPUT);
pinMode(LED2_Red, OUTPUT);

void loop() {

//Read data and store it to variables hum and temp


hum = dht.readHumidity();
temp = dht.readTemperature();
//Print temp and humidity values to serial monitor
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(temp);
Serial.println(" Celsius");
delay(5000); //Delay 5 sec.

if (temp > HigherTemp) //Check if Temperature is higher than


HigherTemp
{
digitalWrite(LED1_Green, LOW);
digitalWrite(LED2_Red, HIGH);
}
else
{
digitalWrite(LED1_Green, HIGH);
digitalWrite(LED2_Red, LOW); }
}
EXPERIMENT THREE: SWITCH CONTROLLED LED
Introduction
Push button switches are inexpensive and versatile components that
have many uses.

In this guide, we will learn how to use a push button switch together
with an Arduino, to turn an LED on and off. The circuit we will be
building, uses a Little Bird Uno R3, a fully compatible Arduino
development board. A mini pushbutton switch, a 5mm LED, jumper
wires, and a mini breadboard is also required.

Other uses for push buttons are in custom gamepads, DIY radio
buttons, MIDI controllers with push buttons that in conjunction with
LEDs would light up when pressed, and many more!
Step 1 Insert LED into the Breadboard

 Insert an LED into the breadboard with the Anode (positive leg) on
the left and the Cathode (negative leg on the right).
Step 2 Insert a 220 ohm resistor

 Insert a 220 Ohm Resistor so that one leg is inline with the LED's
Cathode leg.
 Resistors are not polarised, so orientation doesn't matter.
 This resistor will be used to limit the current going to our LED.

Step 3 Insert the button

 Insert your push button so that one leg is in line with the other end
of the resistor.
 Be sure to really push down on the push button so that the bottom
of the push button is flush with the breadboard (this will feel like
you're pushing too hard).

Step 4 Connect pin 13 to the LED

 Connect pin 13 to the anode of the LED.

Step 5 Connect the resistor to ground

 Connect the resistor to ground.


Step 6 Connect the push button to pin 7

 Connect the push button to pin 7.

Step 7 Program 1: Push to turn on the LED


const int ledPin = 13;// We will use the internal LED
const int buttonPin = 7;// the pin our push button is on

void setup()
{
pinMode(ledPin,OUTPUT); // Set the LED Pin as an output
pinMode(buttonPin,INPUT_PULLUP); // Set the Tilt Switch as an
input
}

void loop()
{
int digitalVal = digitalRead(buttonPin); // Take a reading

if(HIGH == digitalVal)
{
digitalWrite(ledPin,LOW); //Turn the LED off
}
else
{
digitalWrite(ledPin,HIGH);//Turn the LED on
}
}

 Upload this code to your Arduino.


 When you run this code, the LED on Pin 13 will turn on when the
button is held down. That is all.

Step 8 Program 2: Toggle the LED


const unsigned int buttonPin = 7;
const unsigned int ledPin = 13;

int buttonState = 0;
int oldButtonState = LOW;
int ledState = LOW;

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
buttonState = digitalRead(buttonPin);

if (buttonState != oldButtonState &&


buttonState == HIGH)
{
ledState = (ledState == LOW ? HIGH : LOW);
digitalWrite(ledPin, ledState);
delay(50);
}
oldButtonState = buttonState;
}

 Upload this code to your Arduino.


 This program will toggle on and off the LED every time you push the
button.

You might also like