ArduinoIoT

From COINS Research Group
Jump to: navigation, search

Tutorial Overview

Title

Introduction into Arduino and Internet Of Things Applications.

Author

Abdelrahman Mahmoud

Note

For any suggestions/comments about this tutorial, please send an email to akoubaa@coins-lab.org / a.mahmoud1994@gmail.com

Make sure you download:

  • Download your OS version whether it is Windows/Linux/IOS
  • Extract the zip file any where you want
  • Download this library for Temperature sensor
  • Download this library for Ultrasonic sensor

Remember where did you save the libraries and you will learn when and where to use them later in the tutorial.

Components

Description

This tutorial will present some demo for the Arduino platform, how to read data from sensors, write data to sensors and send data to another arduino board through Xbee wireless communication.


Learning Outcomes

  • Develop simple applications with Arduino
  • Develop programs with Arduino sensors
  • Develop applications to communicate between two
  • Arduino sensor boards using Xbee

Getting started with Arduino

Hello World Program

In this example you are going to learn how to print "Hello World" on Serial monitor after uploading the program on the Arduino UNO board.

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

void loop() {
Serial.println("Hello World!!!"); 
delay(1000);              // wait for a second
}

The Blink Program

In this example you are going to control the internal LED on the Arduino UNO board which is connected to pin #13 and will learn how to control the output on one of the pins.

Go to File -> Examples -> Basics -> Blink

void setup() {
 // initialize digital pin 13 as an output.
 pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
 digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
 delay(1000);              // wait for a second
 digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
 delay(1000);              // wait for a second
}

Simple Buzzer Program

In this example you are going to control a buzzer connected to the Arduino UNO board which is connected to pin #8. Place the base shield on top of the arduino board and connect the buzzer to D8 socket via a tinkerkit cable.


void setup() {
 // put your setup code here, to run once:
 Serial.begin(9600);
 pinMode(8,OUTPUT);
}

void loop() {
 // put your main code here, to run repeatedly:
 digitalWrite(8,HIGH);
 delay(500);
 digitalWrite(8,LOW);
 delay(500);

}

Dealing with Arduino Sensors

Simple Push Button Program

 const int pinButton = 3; // the pin the button sensor is connected to 

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

 pinMode(pinButton, INPUT);
}

void loop() {
 
 Serial.println(digitalRead(pinButton));
 delay(1000);
}

Simple Vibration Sensor Program

int sensorPin = A0;    // select the input pin for the sensor

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

void loop() 
{
 Serial.println(analogRead(sensorPin));
}

Simple Grove Gas Sensor Program

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

void loop() 
{
   float vol;
   int sensorValue = analogRead(A2);
   vol=(float)sensorValue/1024;
   Serial.println(vol);
   delay(1000);
}

Simple Light Sensor Program

 
#define LIGHT_SENSOR A0   //Grove - Light Sensor is connected to A0 

void setup() 
{
   Serial.begin(9600);                //Start the Serial connection
}
void loop() 
{
   int sensorValue = analogRead(LIGHT_SENSOR);     
   Serial.print("The sensor data is : ");
   Serial.println(sensorValue);
   delay(1000);
}

Simple Temperature and Humidity Sensor Program

For this program you will need to download this library

Go to Scetch -> Include Library -> Add .Zip Library go to the path where you downloaded the zip file and press select


#include "DHT.h"

#define DHTPIN A0 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11
 
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
   Serial.begin(9600);
   Serial.println("DHTxx test!");

   dht.begin();
}

void loop()
{
   // Reading temperature or humidity takes about 250 milliseconds!
   // Sensor readings may also be up to A0 seconds 'old' (its a very slow sensor)
   float h = dht.readHumidity();
   float t = dht.readTemperature();

   // check if returns are valid, if they are NaN (Not a Number) then something went wrong!
   if (isnan(t) || isnan(h))
   {
       Serial.println("Failed to read from DHT");
   }
   else
   {
       Serial.print("Humidity: ");
       Serial.print(h);
       Serial.print(" %\t");
       Serial.print("Temperature: ");
       Serial.print(t);
       Serial.println(" *C");
   }
}

Simple Ultrasonic Sensor Program

For this program you will need to download this library

Go to Scetch -> Include Library -> Add .Zip Library go to the path where you downloaded the zip file and press select

#include "Ultrasonic.h"

//The pin number the sensor is connected to D7
Ultrasonic ultrasonic(7);

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

void loop()
{
 long rangeInCentimeters;
 rangeInCentimeters = ultrasonic.MeasureInCentimeters();
 Serial.print("The distance is : ");
 Serial.println(rangeInCentimeters);
 delay(500);
}

Leveraging IoT Potential with Arduino and XBee Modules

Simple Blink Program Using Xbee Modules

For the transmitter board


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

void loop()
{
 Serial.println("H");
 delay(1000);
 Serial.println("L");
 delay(1000);
}

For the receiver board Go to File -> Examples -> Communication -> PhysicalPixel


const int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into

void setup() {
 // initialize serial communication:
 Serial.begin(9600);
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);
}

void loop() {
 // see if there's incoming serial data:
 if (Serial.available() > 0) {
   // read the oldest byte in the serial buffer:
   incomingByte = Serial.read();
   // if it's a capital H (ASCII 72), turn on the LED:
   if (incomingByte == 'H') {
     digitalWrite(ledPin, HIGH);
   }
   // if it's an L (ASCII 76) turn off the LED:
   if (incomingByte == 'L') {
     digitalWrite(ledPin, LOW);
   }
 }
}

Simple Humidity Sensor Program Using Xbee Modules

For the transmitter board


#include "DHT.h"
#include <stdio.h>

#define DHTPIN 3 // what pin we're connected to

#define DHTTYPE DHT11 // DHT 11

int floatVariable;


DHT dht(DHTPIN, DHTTYPE);

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

char buff[100]; // we're going to use this to hold our string

void loop()
{
   // Reading temperature or humidity takes about 250 milliseconds!
   // Sensor readings may also be up to A0 seconds 'old' (its a very slow sensor)
   float h = dht.readHumidity();
   float t = dht.readTemperature();

   // check if returns are valid, if they are NaN (not a number) then something went wrong!
   if (isnan(t) || isnan(h))
   {
   }
   else
   {
       sprintf(buff, "%d", 
         int(h));  // and finally the integer
       Serial.write(buff);
       Serial.write("\n");
       delay(2000);
   }
}

For the receiver board


#include <stdio.h>

void printbuffer(char *buffer){
 while(*buffer){
   Serial.write(*buffer++);
 }
 Serial.write("\n");
}

void setup() {
 // initialize serial communication:
 Serial.begin(9600);
}

void loop() {
 char str[4] = {'\0'};

if (Serial.available()>=3){
for (byte i=0; i<3; i++){
  str[i] = Serial.read();
}
printbuffer(str);
}
}

Small Quizzes

Quiz 1

/**
* In this Quiz you have a couple of errors, it could be a syntax
* or a logic error.
* After you finish show any of the instructors your work before
* going to the second quiz.
* Wish you all the best of luck :D
*/
void setup() 
{

Serial.begin(9600);

} 


void loop() 
{

Serial.println("Welcome to the Arduino Workshop");
Serial.println("This is your first Arduino Quiz :D "');
Serial.println("Type your Group number NEXT to this line");
           

}

Quiz 2

/*
* Choose Only one program from below:
*
*(1) Write a program that turn off the build in Green LED 
* if the distance between an Ultrasonic sensor and an object 
* is less than 10 cm and turn it on otherwise.
*  
* (2) Write a program that Alerts you using a Buzzer if 
*     the door is open. (Hint: Use the vibration sensor to detect 
 *     if the door is open)
* 
* (3) Write a program that Alerts you using a Buzzer if 
*     there is a Gas smell. (Hint: Use the gas sensor to detect 
*     if there is a gas leak) 
*     
* (4) Write a program that turn on a Green LED if 
*     it is Dark.(Hint: Use the Light sensor to detect 
*     light)
*/

void setup() {
 // put your setup code here, to run once:

}

void loop() {
 // put your main code here, to run repeatedly:

}

Quiz 3

/*
* Good Job!!! You have reached the third and last quiz of this 
* workshop.
* 
* This quiz depends on the IOT concept and the fastest group to solve 
* it will win a PRIZE. 
*
* Write a program that sends in the Serial (Hint: Send your answer in one line and words   
separated by spaces): 
   * Choose a Group name
    * Your Group number
    
* throught the serial port to be able to receive this data from 
* another device using XBee connection
* 
*/
 
void setup() {
 // put your setup code here, to run once:

}

void loop() {
 // put your main code here, to run repeatedly: 

}
 

Important Notes

  • Make sure that you selected the right port number.
  • Make sure that you choose the right board name.

Executing step

1.Compile the program to see if there is any kind of errors.

2. Run this command (For Linux users) : sudo chmod 777 /dev/ttyUSB0

To give the permission to the port you are using. Note: make sure you give the permission to the right port.

3.Upload the arduino program to the arduino board and make sure you picked the right Board and the right Serial Port.

4. Open the Serial Monitor to be able to see the received data.

Additional References

  • Coursera Arduino Platform and C programming course
  • Coursera Interfacing with the Arduino course
  • Arduino course