0% found this document useful (0 votes)
33 views34 pages

Iot Programming

The document outlines a series of experiments involving Arduino programming, including installation of the Arduino IDE, adding libraries, and writing various programs such as blinking an LED, fading an LED, reading digital input, and displaying text on an LCD. Each experiment includes objectives, required apparatus, procedures, and expected results. The document serves as a practical guide for learning Arduino programming and electronics.

Uploaded by

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

Iot Programming

The document outlines a series of experiments involving Arduino programming, including installation of the Arduino IDE, adding libraries, and writing various programs such as blinking an LED, fading an LED, reading digital input, and displaying text on an LCD. Each experiment includes objectives, required apparatus, procedures, and expected results. The document serves as a practical guide for learning Arduino programming and electronics.

Uploaded by

zoro39708
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

INDEX

S No. Programs Signature


1. Study and Install IDE of Arduino
2. Write the steps to add libraries in Arduino
and setup of Arduino IDE for programming.
3. Write a Program using Arduino for Blink
LED.
4. Write a program to shows how to fade an
LED on pin 9 using the analogWrite( )
function.
5. Reads a digital input on pin 2, prints the
result to the serial monitor.
6. Write a program to print “ Hello World” on
LCD Display using Arduino Uno Board.
7. To design the Voltmeter Using Arduino Uno
Board.
8. Write a Program to rotate the Servomotor
From 0 to 180 degree.
9. Write the steps to add blynk libraries for
NodeMCU and account on IFTTT for home
automation
10. Write a Program for Controlling Raspberry
Pi with WhatsApp.
Experiment No. 1
Objective: To Study the Arduino board and Install IDE of Arduino.
Apparatus:
S.No. Apparatus Quantity
1. Arduino board 1

Procedure:
1. Arduino Board (e.g., Arduino Uno, Mega, Nano)
2. USB Cable (type depends on the Arduino model)
3. Computer (Windows, macOS, or Linux)
4. Breadboard (optional, for prototyping circuits)
5. Jumper Wires (optional, for connecting components)
6. LEDs, Resistors, Sensors, etc. (optional, for experiments and projects)
Steps to Study the Arduino Board:
1. Understanding the Arduino Board:
- Microcontroller: The brain of the Arduino board, typically an Atmel
AVR microcontroller.
- Digital Pins: Used for digital input and output.
- Analog Pins: Used for analog input.
- Power Pins: Used to power the board and connected components.
- USB Port: For uploading code and powering the board.
- Power Jack: For external power supply.
- Reset Button: To reset the microcontroller.
- On-board LEDs: Indicate power, communication, and pin status.
2. Reading Documentation:
- Visit the official Arduino website and read through the
https://store.arduino.cc/products/arduino-uno-rev3 documentation or the
documentation for your specific model.
3. Basic Circuit Prototyping:
- Start with simple projects like blinking an LED to understand basic
input/output operations.
Steps to Install the Arduino IDE:
1. Download the Arduino IDE:
- Go to the [Arduino Software page](https://www.arduino.cc/en/software)
and download the appropriate version for your operating system (Windows,
macOS, Linux).

2. Install the Arduino IDE:


- Windows:
- Run the downloaded `.exe` file and follow the installation instructions.
- macOS:
- Open the downloaded `.dmg` file and drag the Arduino application into
the Applications folder.
- Linux:
- Extract the downloaded `.tar.xz` file and run the `install.sh` script in the
terminal.
3. Connect the Arduino Board:
- Connect your Arduino board to the computer using the USB cable.

4. Configure the Arduino IDE:


- Open the Arduino IDE.
- Go to Tools > Board and select your Arduino board model.
- Go to Tools > Port and select the appropriate port (the one corresponding
to your Arduino board).
A − Used to check if there is any compilation error.

B − Used to upload a program to the Arduino board.

C − Shortcut used to create a new sketch.

D − Used to directly open one of the example sketch.

E − Used to save your sketch.

F − Serial monitor used to receive serial data from the board and send the serial
data to the board.
5. Install Arduino Drivers (if needed):
- On Windows, drivers should install automatically when the board is
connected. If not, you can manually install them from the Arduino IDE
installation directory.
- On macOS and Linux, additional drivers are generally not needed, but
you may need to grant permission to access the USB port.
Uploading Your First Sketch:
1. Open an Example Sketch:
- Go to File > Examples > 01. Basics > Blink.
2. Verify the Sketch:
- Click the checkmark button (✔) to verify/compile the code.
3. Upload the Sketch:
- Click the right arrow button (→) to upload the code to the Arduino board.
- The onboard LED should start blinking, indicating the code has been
successfully uploaded.

RESULT: In the experiment the Arduino Board is studied as well as


Installation of IDE is done and studied.
Experiment No. 2
Objective: Write the steps to add libraries in Arduino and setup of Arduino
IDE for programming.
Apparatus:
S.No. Apparatus Quantity
1. Arduino Uno board IDE 1

Procedure:

Libraries are a collection of code that makes it easy for you to connect to a
sensor, display, module, etc. For example, the Liquid Crystal library makes it
easy to talk to character LCD displays.

There are thousands of libraries available for download directly through the
Arduino IDE

Using the Library Manager

To install a new library into your Arduino IDE you can use the Library
Manager (available from IDE version 1.6.2). Open the IDE and click to the
"Sketch" menu and then Include Library > Manage Libraries.

Then the Library Manager will open and you will find a list of libraries that
are already installed or ready for installation.
In this example we will install the Liquid Crystel Library library. Scroll the list
to find it, click on it, then select the version of the library you want to install.
Sometimes only one version of the library is available. If the version selection
menu does not appear, don't worry: it is normal.

Finally click on install and wait for the IDE to install the new library.
Downloading may take time depending on your connection speed. Once it has
finished, an Installed tag should appear next to the LiquidCrystal library. You
can close the library manager.
You can now find the new library available in the Sketch > Include
Library menu.

(a) If you want to add your own library to Library Manager,

Importing a .zip Library

Libraries are often distributed as a ZIP file or folder. The name of the folder is
the name of the library. Inside the folder will be a .cpp file, a .h file and often a
keywords.txt file, examples folder, and other files required by the library.
Starting with version 1.0.5, you can install 3rd party libraries in the IDE. Do
not unzip the downloaded library, leave it as is.

In the Arduino IDE, navigate to Sketch > Include Library > Add .ZIP Library.
At the top of the drop-down list, select the option to "Add .ZIP Library''.

You will be prompted to select the library you would like to add. Navigate to
the .zip file's location and open it.
Return to the Sketch > Include Library menu. . You should now see the library
at the bottom of the drop-down menu. It is ready to be used in your sketch.
The zip file will have been expanded in the libraries folder in your Arduino
sketches directory.

RESULT: In the experiment the Installation of various in build and external


libraries required for programming in IDE is done and steps are
studied.
Experiment No. 3
Objective: Write a Program using Arduino for Blink LED.
Procedure:

(a)LED Specifications
LED’s are kind of semiconductors with the light sources in which both P-
type(holes) and Ntype(electrons) semiconductors will combine. By giving some
certain forward voltage this combination of holes and electrons occurs,
releasing the form of which is light. The wastage of electricity is very less in
LED’s and it directly converts electrical energy into light energy.

Figure shows a view of LED.

Pin definition

Long Pin +5V


Short Pin GND
Hardware Required:
S.No. Component Quantity
1. Arduino UNO 1
2. LED 1
3. Resistor 220Ω 1
4. Breadboard 1
5. USB 2.0 Cable Type A/B 1
6. Jumper Wires As required
(b) 7. Personel Computer 1
Using Tinker CAD:
❖ Login to TinkerCAD.com using your mail id.
❖ After logging in, go to the domain Design in Tinker CAD and create a New
Project.
❖All the components will be available on the right side of the window.
❖ Pick, place and connect the components as per the diagram.

❖ After the connections are made, click the Start Simulation button.
(b)Writing Sketch:
Once the circuit connection are ready, programming the Arduino can be done in
three ways.
o Using code blocks
o Using code blocks + text programming
o With text program Now from the code menu select blocks.
The default program to blinking led with code blocks is provided. press start
simulation, you will notice the blinking of LED with 1sec.
Now from the code menu select the text programming mode and change
program to blink the led.
1. void setup()
2. {
3. pinMode(LED_BUILTIN, OUTPUT);
4. }
5. void loop()
6. {
7. // turn the LED on (HIGH is the voltage level)
8. digitalWrite(LED_BUILTIN, HIGH);
9. delay(1000); // Wait for 1000 millisecond(s)
10.// turn the LED off by making the voltage LOW
11.digitalWrite(LED_BUILTIN, LOW);
12.delay(1000); // Wait for 1000 millisecond(s)
13.}

RESULT: In the experiment the led blinks after every second as objective of
the experiment.
Experiment No. 4
Objective: Write a program to shows how to fade an LED on pin 9 using
the analogWrite() function.
Procedure:
(a)LED Specifications
LED’s are kind of semiconductors with the light sources in which both P-
type(holes) and Ntype(electrons) semiconductors will combine. By giving some
certain forward voltage this combination of holes and electrons occurs, releasing
the form of which is light. The wastage of electricity is very less in LED’s and it
directly converts electrical energy into light energy

Figure shows a view of LED.

Pin definition

Long Pin +5V


Short Pin GND
Hardware Required:
S.No. Component Quantity
1. Arduino UNO 1
2. LED 1
3. Resistor 220Ω 1
4. Breadboard 1
5. USB 2.0 Cable Type A/B 1
6. Jumper Wires As required
7. Personel Computer 1 (b) Using Tinker CAD:
❖Login to TinkerCAD.com using your mail id.
❖ After logging in, go to the domain Design in TinkerCAD and create a New
Project.
❖All the components will be available on the right side of the window.
❖ Pick, place and connect the components as per the diagram.

❖After the connections are made, click the Start Simulation button.
(c) Writing Sketch:
Once the circuit connection are ready, programming the Arduino can be done in
three ways.
o Using code blocks
o Using code blocks + text programming
o With text program Now from the code menu select blocks.
The default program to blinking led with code blocks is provided. press start
simulation, you will notice the blinking of LED with 1sec.
Now from the code menu select the text programming mode and change
program to blink the led.

Sketch : (c++)
/* The analogWrite() function uses PWM, so if you
want to change the pin you're using, be sure to
use another PWM capable pin. On most Arduino,
the PWM pins are identified with a "~" sign,
like ~3, ~5, ~6, ~9, ~10 and ~11.
*/
1. int brightness = 0;
2. void setup()
3. {
4. pinMode(9, OUTPUT);
5. }
6. void loop()
7. {
8. for (brightness = 0; brightness <= 255; brightness += 5) {
9. analogWrite(9, brightness);
10.delay(30); // Wait for 30 millisecond(s)
11.}
12.for (brightness = 255; brightness >= 0; brightness -= 5) {
13.analogWrite(9, brightness);
14.delay(30); // Wait for 30 millisecond(s)
15.}
16.}

RESULT: In the experiment the led intensity changesas objective of the


experiment.
Experiment No. 5
Objective: Reads a digital input on pin 2, prints the result to the serial
monitor.
Apparatus:

S.No. Component Quantity


1. Arduino UNO 1
2 USB 2.0 Cable Type A/B 1
3 Jumper Wires As required
4 Personel Computer 1

Procedure:
1. Connect Components:
- Connect one end of a jumper wire to pin 2 (digital input) of the Arduino
board.
- Connect the other end of the jumper wire to one terminal of a push button.
- Connect a second jumper wire from the other terminal of the push button to
the ground (GND) pin on the Arduino board.
2. Launch Arduino IDE:
- Open the Arduino IDE on your computer.
3. Write the Sketch:
- Write the following code in the Arduino IDE:
Sketch : (c++)
1. const int buttonPin = 2; // Define the pin for the button
2. void setup() {
3. Serial.begin(9600); // Initialize serial communication at 9600 baud
4. pinMode(buttonPin, INPUT); // Set button pin as input
5. }
6. void loop() {
7. int buttonState = digitalRead(buttonPin); // Read the state of the
button
8. Serial.print("Button state: ");
9. Serial.println(buttonState); // Print the button state to the serial
monitor
10. delay(100); // Delay for stability
11. }
4. Upload the Sketch:
- Connect your Arduino board to your computer using the USB cable.
- Select the correct board and port under Tools > Board and Tools > Port
respectively.
- Click the right arrow icon (→) in the Arduino IDE to compile and upload the
sketch to your Arduino board.
5. Open Serial Monitor:
- After uploading the sketch, go to Tools > Serial Monitor in the Arduino IDE
menu.
- Alternatively, press `Ctrl + Shift + M` on your keyboard.
6. Observe the Output:
- Once the Serial Monitor opens, you should see the state of the button (HIGH
or LOW) printed continuously.
- Press the button and observe how the state changes in the serial monitor.
Result:
- The Arduino will continuously read the digital input from pin 2, representing
the state of the push button (pressed or not pressed).
- The result (button state) will be printed to the serial monitor in real-time.
- You will observe the state change when the button is pressed or released,
reflecting as either "1" (HIGH) or "0" (LOW) in the serial monitor.
Experiment No. 6
Objective: Write a program to print “ Hello World” on LCD Display using
Arudino UnoBoard.
Apparatus:

S.No. Component Quantity


1. Arduino UNO 1
2 USB 2.0 Cable Type A/B 1
3 Jumper Wires As required
4 LCD Display Module (compatible with 1
HD44780 driver)
5 Breadboard 1
6 Potentiometer (for contrast adjustment) 1

Procedure:
1. Circuit Connection:
- Connect the LCD display module to the Arduino Uno board as follows:
- VSS pin of the LCD to GND
- VDD pin of the LCD to 5V
- VO pin of the LCD to the wiper terminal of the potentiometer
- RS pin of the LCD to digital pin 12 of the Arduino
- RW pin of the LCD to GND
- Enable (E) pin of the LCD to digital pin 11 of the Arduino
- D4-D7 pins of the LCD to digital pins 5-2 of the Arduino
- A (anode) pin of the backlight to 5V
- K (cathode) pin of the backlight to GND
- Connect the other two terminals of the potentiometer to 5V and GND.
2. Include Library:
- Open the Arduino IDE.
- Go to Sketch > Include Library > LiquidCrystal and select LiquidCrystal to
include the necessary library.

3. Write the Sketch:


- Write the following code in the Arduino IDE:
#include <LiquidCrystal.h>
int seconds = 0;
LiquidCrystal lcd_1(12, 11, 5, 4, 3, 2);
void setup()
{
}
void loop()
{
lcd_1.begin(16, 2);
lcd_1.print("hello world!");
}
4. Upload the Sketch:
- Connect your Arduino Uno board to your computer using a USB cable.
- Select the correct board and port under Tools > Board and Tools > Port
respectively.
- Click the right arrow icon (→) in the Arduino IDE to compile and upload the
sketch to your Arduino board.
5. Observe Output:
- After uploading the sketch, you should see "Hello World" displayed on the
LCD module.

Result:
- "Hello World" will be displayed on the LCD module connected to the Arduino
Uno board.
Experiment No. 7
Objective: To design the Voltmeter Using Arduino Uno Board.
Apparatus:

S.No. Component Quantity


1. Arduino UNO 1
2 Voltage Divider Circuit Components: 1
- Resistors (e.g., 10kΩ and 10kΩ)
3 Jumper Wires As required
4 Voltage source (to measure) 1
5 Breadboard 1

Procedure:
1. Build Voltage Divider Circuit:
- Connect two resistors in series to form a voltage divider circuit. For
example, use two 10kΩ resistors.
- Connect one end of the first resistor to 5V (VCC) of the Arduino Uno.
- Connect the other end of the first resistor to the input voltage (Vin) you want
to measure.
- Connect the junction between the two resistors to an analog pin (e.g., A0) of
the Arduino Uno.
- Connect the other end of the second resistor to the ground (GND) of the
Arduino Uno.
2. Write the Sketch.
1. #include <LiquidCrystal.h>
2. const int analogInputPin = A0; // Analog pin connected to the voltage
divider output
3. void setup() {
4. Serial.begin(9600); // Initialize serial communication
5. void loop() {
6. int rawValue = analogRead(analogInputPin); // Read raw ADC value
7. float voltage = rawValue * (5.0 / 1023.0); // Convert ADC value to
voltage (5V reference, 10-bit resolution)
8. // Print the voltage to the serial monitor
9. Serial.print("Voltage: ");
10.Serial.print(voltage);
11.Serial.println(" V");
12.delay(1000); // Delay for stability
13.}
3. Upload the Sketch:
- Connect your Arduino Uno board to your computer using a USB cable.
- Select the correct board and port under Tools > Board and Tools > Port
respectively.
- Click the right arrow icon (→) in the Arduino IDE to compile and upload the
sketch to your Arduino board.
4. Open Serial Monitor:
- After uploading the sketch, go to Tools > Serial Monitor in the Arduino IDE
menu.
- Alternatively, press `Ctrl + Shift + M` on your keyboard.
5. Observe Output:
- Once the Serial Monitor opens, you should see the measured voltage
displayed in volts.

Result:
- The Arduino Uno board will continuously read the analog voltage from the
voltage divider circuit and convert it into a readable voltage value.
- The measured voltage will be displayed in volts on the Serial Monitor.
Experiment No. 8
Objective: Write a Program to rotate the Servomotor From 0 to 180 degree.
Apparatus:

S.No. Component Quantity


1. Arduino UNO 1
2 Jumper Wires As required
3 Servo Motor 1

Procedure:
1. Connect Servo Motor:
- Connect the signal pin of the servo motor to digital pin 9 of the Arduino
Uno.
- Connect the power (Vcc) and ground (GND) pins of the servo motor to the
appropriate pins on the Arduino (5V and GND respectively).

2. Include Servo Library:


- Open the Arduino IDE.
- Go to Sketch > Include Library > Servo and select Servo to include the
necessary library.
3. Write the Sketch:
1. #include <Servo.h>
2. Servo myServo; // Create a servo object
3. void setup() {
4. myServo.attach(9); // Attach the servo to digital pin 9
5. }
6. void loop() {
7. // Rotate servo from 0 to 180 degrees in steps of 1 degree
8. for (int angle = 0; angle <= 180; angle++) {
9. myServo.write(angle); // Set servo position
10.delay(15); // Delay for servo to reach the position
11.}
12.// Rotate servo from 180 to 0 degrees in steps of 1 degree
13.for (int angle = 180; angle >= 0; angle--) {
14.myServo.write(angle); // Set servo position
15.delay(15); // Delay for servo to reach the position
16.}
17.}
4. Upload the Sketch:
- Connect your Arduino Uno board to your computer using a USB cable.
- Select the correct board and port under Tools > Board and Tools > Port
respectively.
- Click the right arrow icon (→) in the Arduino IDE to compile and upload the
sketch to your Arduino board.
5. Observe Servo Movement:
- After uploading the sketch, the servo motor will start rotating from 0 to 180
degrees and then back to 0 degrees continuously.

Result:
- The servo motor connected to digital pin 9 of the Arduino Uno board will
rotate from 0 to 180 degrees and then back to 0 degrees in steps of 1 degree
continuously.
Experiment No. 9
Objective: Write the steps to add blynk libraries for NodeMCU and
account on IFTTT for home automation
Procedure:
Adding Blynk Libraries for NodeMCU:
1. Download Blynk Library:
- Open the Arduino IDE.
- Go to Sketch > Include Library > Manage Libraries....
- In the Library Manager, search for "Blynk" and select the latest version of
the Blynk library.
- Click the "Install" button to install the library.
2. Install ESP8266 Board Package:
- Go to File > Preferences in the Arduino IDE.
- In the "Additional Board Manager URLs" field, add the following URL:
`http://arduino.esp8266.com/stable/package_esp8266com_index.json`
- Click "OK" to close the Preferences window.
- Go to Tools > Board > Boards Manager....
- Search for "esp8266" and install the ESP8266 board package by ESP8266
Community.
3. Select NodeMCU Board:
- After installing the ESP8266 board package, go to Tools > Board and select
"NodeMCU 1.0 (ESP-12E Module)" or your specific NodeMCU board variant.
4. Verify Installation:
- Create a new sketch or open an example sketch from File > Examples >
Blynk > Boards_WiFi > ESP8266_Standalone.
- Verify that the Blynk library is correctly included, and the NodeMCU board
is selected.

Creating an Account on IFTTT for Home Automation:


1. Visit IFTTT Website:
- Go to the [IFTTT website](https://ifttt.com/) in your web browser.
2. Sign Up or Log In:
- If you already have an IFTTT account, log in using your credentials.
- If you don't have an account, sign up for a new account by providing your
email address and creating a password.
3. Explore Applets:
- Once logged in, explore the available applets or create your own by
combining different services and triggers.
4. Create New Applet:
- To create a new applet, click on your profile icon at the top right corner and
select "Create".
- Click on "+This" to choose a trigger service. This could be a smart home
device, a social media platform, or any other supported service.
- Follow the prompts to select the trigger event and configure its settings.
5. Choose Action:
- After selecting the trigger, click on "+That" to choose an action service.
- Search for and select the action service you want to perform when the trigger
event occurs.
- Configure the action settings as needed.
6. Complete Applet:
- Review the applet settings to ensure everything is configured correctly.
- Click on "Finish" or "Create" to save and activate the applet.
7. Explore More:
- Explore more applets or create additional ones to automate various tasks in
your home or workflow.
Experiment No. 10
Objective: Write a Program for Controlling Raspberry Pi with WhatsApp.
Requirements:
1. Raspberry Pi with internet connectivity
2. Twilio account (for sending/receiving WhatsApp messages)
3. Python installed on Raspberry Pi
4. Twilio Python library (`twilio`)
Steps:
1. Set up Twilio:
- Sign up for a Twilio account if you don't have one already.
- Navigate to the Twilio Console and obtain your Account SID and Auth
Token.
- Configure a Twilio phone number for WhatsApp messaging.

2. Install Twilio Python Library:


- Open a terminal on your Raspberry Pi.
- Install the Twilio Python library using pip:
1. pip install twilio
3. Write Python Script:
- Create a Python script (e.g., `whatsapp_control.py`) on your Raspberry Pi.
- Use the Twilio Python library to set up a webhook to receive incoming
messages from WhatsApp.
- Parse the incoming messages and trigger actions on the Raspberry Pi
accordingly.
2. from flask import Flask, request
3. from twilio.twiml.messaging_response import MessagingResponse
4. app = Flask(__name__)

5. @app.route("/whatsapp", methods=['POST'])
6. def respond_to_whatsapp():
7. incoming_msg = request.values.get('Body', '').lower()
8. resp = MessagingResponse()
9. if 'on' in incoming_msg:
10.# Code to turn on a specific device or execute a command
11.resp.message("Turning on device...")
12.elif 'off' in incoming_msg:
13.# Code to turn off a specific device or execute a command
14.resp.message("Turning off device...")
15.else:
16.resp.message("Invalid command. Please send 'on' or 'off' to control the
device.")
17.return str(resp)
18.if __name__ == "__main__":
19.app.run(debug=True)
4. Configure Webhook URL:
- Expose your Python script to the internet using a service like ngrok or host it
on a server with a public URL.
- Configure the webhook URL in your Twilio Console to point to your Python
script (e.g., `http://your-domain.com/whatsapp`).
5. Test:
- Send a WhatsApp message to your Twilio phone number with the command
(e.g., "on" or "off").
- Check if the Raspberry Pi responds accordingly.
6. Expand Functionality:
- Expand the Python script to handle more commands or trigger more actions
based on the incoming messages.
- Implement error handling and security measures as needed.
Notes:
- Ensure your Raspberry Pi has internet connectivity and can access external
APIs like Twilio.
- Test the script thoroughly to ensure proper functionality and error handling.
- Consider security implications, especially if your Raspberry Pi is controlling
sensitive devices or systems.

You might also like