IoT Tutorials

From COINS Research Group
Revision as of 08:13, 22 March 2016 by Abdelrahman (Talk | contribs) (Components)

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

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

The Blink Program

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 Push Button Program

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

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

 pinMode(pinButton, INPUT);
}

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

Simple Buzzer Program

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);

}

Simple Vibration Sensor Program

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

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

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

Simple Grove Temperature Sensor Program

#include <math.h>

int dataFromSensor;
float temperature;
int thermistorValue=3975;                  //B value of the thermistor
float resistance;

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

void loop()
{
 //Connected to pin 0
   dataFromSensor=analogRead(0);
   
   resistance=(float)(1023-dataFromSensor)*10000/dataFromSensor; //get the resistance of the sensor;
   temperature=1/(log(resistance/10000)/thermistorValue+1/298.15)-273.15;//convert to temperature via datasheet ;
   
   delay(1000);
   //Serial.print("The temperature is : ");
   Serial.println(temperature);
}

Simple Light Sensor with LED Program

#include <math.h>

#define LIGHT_SENSOR A0   //Grove - Light Sensor is connected to A0 
const int ledPin=6;       //Connect the LED Grove module to Digital Pin6

#define RANGE_VALUE 50    //Define your range value that the LED state will depend on

void setup() 
{
   Serial.begin(9600);                //Start the Serial connection
   pinMode(ledPin,OUTPUT);            //Set the LED on Digital 12 as an OUTPUT
}
void loop() 
{
   int sensorValue = analogRead(LIGHT_SENSOR); 
   if(sensorValue > RANGE_VALUE)
   digitalWrite(ledPin, HIGH);
   else
   digitalWrite(ledPin, LOW);
   
   Serial.print("the analog read data is ");
   Serial.println(sensorValue);
   delay(500);
}

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
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);
}

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;

int frac(float num){
 return( ((num + .005) - (int)num) * 100);
}


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);
//Serial.println(atof(str));
}
}

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 : 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