Iot Programming
Iot Programming
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).
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.
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
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.
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.
(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.
Pin definition
❖ 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
Pin definition
❖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.}
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:
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.
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:
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:
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).
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.
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.