Print Merged
Print Merged
if (condition)
{
// do something here
}
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 20 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 21
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 22 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 23
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 24 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 25
if-else-if Syntax if-else-if Example
if (condition) { if (x > 120) {
do Something;} digitalWrite(LEDpin1, HIGH);
else if (condition2) { digitalWrite(LEDpin2, HIGH);
do Something Else;} } else if (x > 200) {
else { digitalWrite(LEDpin1, LOW);
do Another Thing;} digitalWrite(LEDpin2, HIGH);
} else {
digitalWrite(LEDpin1, LOW);
digitalWrite(LEDpin2, LOW);
}
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 26 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 27
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 28 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 29
bailout:
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 30 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 31
While Loop Statement While Loop Syntax
while(expression) {
// statement(s)
}
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 33 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 34
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 35 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 36
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 37 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 38
For Loop Statement For Loops Syntax
for (initialization; condition; increment) {
//statement(s);
}
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 39 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 40
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 41 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 42
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 43 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 45
Comparison Operators Boolean Operators
&& (and)
== (equal to) || (or)
!= (not equal to) ! (not)
< (less than)
> (greater than)
<= (less than or equal to)
>= (greater than or equal to)
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 46 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 47
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 48 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 50
Comment
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 52 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 53
#define #include
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 54 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 55
Variable Resistor
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 62 EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 64
pinMode(pin, INPUT/OUTPUT)
Digital Output
Eg
digitalWrite(9,HIGH)
delay
Pauses the program for the amount of
time (in miliseconds) specified as
parameter
Syntax
delay(ms)
Blink LED
// the setup function runs once when you press
reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an
output.
pinMode(12, OUTPUT);
}
Analog Output
Analog Output
A few PINs on the Arduino Digital PINs allow to
modify the output to mimic analog signal
Pin 3, 5, 6, 9, 10, 11
analogWrite Syntax
Syntax
analogWrite(PIN, value)
Eg
analogWrite(9, 125)
Time: 10 mins
Analog Input
Eg
analogRead(A0)
Exercise: analogRead
Write a simple sketch to read the analog output
of a potentiometer and output to the serial
monitor
Time: 10 mins
A0
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 90
Map
Syntax:
map(value, fromLow, fromHigh, toLow, toHigh)
Eg
Map
10KΩ
● Connect the circuit shown on
the left
● Write a sketch from bare
minimum to turn on the LED
Time: 15mins when the button is pressed
• The frequency of the control signal should be 50Hz i.e. a pulse should
occur every 20ms.
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani
5 6
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani
8 9
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani
Steps for connecting Servo motor and potentiometer with Arduino Servo Motor
BITS Pilani BITS Pilani
1. The servo has a three pin female connector. The brown (or darkest pin) is
usually ground. Connect this to ground of Arduino.
Potentiometer
3. Connect the Yellow pin on the servo to Arduino’s digital pin 11.
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani
Servo Motor
Potentiometer in Servo Motor Interfacing
BITS Pilani BITS Pilani
• Thus, the position of the potentiometer precisely controls the servo motor’s
rotation.
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach11); // attaches the servo on pin11 to the servo object
}
void loop() {
val = analogRead(potpin);
// reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180);
// scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15);
} EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 3
PulseIn
Reads a pulse (either HIGH or LOW) on a pin. For
example, if value is HIGH, pulseIn() waits for the
pin to go HIGH.
Syntax:
pulseIn(pin, value)
pulseIn(pin, value, timeout)
Ex: ultrasonics
EEE F411, IoT course, Dr. Vinay Chamola, BITS-Pilani 9
void setup() {
Serial Communication Serial.begin(9600); //set up serial library baud rate to 9600
A serial bus consists of just two wires - the transmitter TX wire for sending data and receiver RX
wire for receiving data. }
void loop() {
if(Serial.available()) //if number of bytes (characters) available for reading from serial port
{
Serial.print(" I received: "); //print- I received
Serial.write(Serial.read()); //send what you read
}
}
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available() == 0);
int val = Serial.read() -'0';
Serial Communication on Arduino/ NodeMCU - Serial Serial.println(val);
}
Monitor
Some basic functions: Example 3: In this example, we will learn how to send messages to a computer via a USB port.
● Serial.begin(baudrate) - This function is used to set the speed of transferring data at a
specific baud rate. Put this inside the setup function. Code:
● Serial.print() - This function converts the data in the ASCII text which is easily readable // serial_print
by human beings and prints it on the serial monitor.
● Serial.println() - This function works similarly to print() but in addition, it adds a new line int number;
● Serial.write() - Writes binary data to the serial port. This data is sent as a byte or series
of bytes; to send the characters representing the digits of a number use the print() void setup() {
function instead. number = 0;
● Serial.read() - This function is used to read incoming serial data (i.e. can read input data Serial.begin(9600); // Initialize serial communication at 9600 bits per second
from user) }
● Serial.available() - This function gets the number of bytes available for reading from the
serial port. We will usually use this inside an if statement to check whether we have data void loop() {
to read or not. For eg: if(Serial.available() > 0) { … }. number++;
Serial.print("Hello World! "); // Next print would happen on the same line
Serial.println(number % 10); // Next print would happen on a new line
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ThingSpeak.h>
#define DHTPIN D5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "abcde"; //Enter your Wi-Fi hotspot ID
const char* password = "123456789"; //Enter your Wi-Fi hotspot password here
WiFiClient client;
unsigned long myChannelNumber = 12345; // Enter your channel ID
const char * myWriteAPIKey = "FALKDFJALDLAS"; //Paste your Write API here
uint8_t temperature, humidity;
void setup()
{
Serial.begin(115200);
dht.begin();
delay(10);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
}
void loop()
{
static boolean data_state = false;
temperature = dht.readTemperature();
humidity = dht.readHumidity();
Serial.print("Temperature Value is :");
Serial.print(temperature);
Serial.println("C");
Serial.print("Humidity Value is :");
Serial.print(humidity);
Serial.println("%");
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store
up to 8 different
// pieces of information in a channel. Here, we write to field 1.
if( data_state )
{
ThingSpeak.writeField(myChannelNumber, 1, temperature, myWriteAPIKey);
data_state = false;
}
else
{
ThingSpeak.writeField(myChannelNumber, 2, humidity, myWriteAPIKey);
data_state = true;
}
delay(30000); // ThingSpeak will only accept updates every 15 seconds.
}
Connections
• LED Pos -> Pin 11 (GPIO17)
• Led Neg -> Ground (Pin 6)
Servo Motor
• A servo motor is a type of a DC motor that, upon
receiving a signal of certain frequency , can rotate
itself to any angle from 0- 180 degrees.
Code: Servo
Motor Control
Temperature • The DHT 11 is a temperature and a Humidity
and sensor that provides digital temperature and
humidity readings.
Humidity • Popular for use in remote weather stations, soil
monitors and home automation systems.
Sensor
Connections
RPI
1. Request Data
Code:
Interfacing
DHT11 Sensor