Hey, Welcome to Rajasthan Polytechnic (BTER) . This blog post is provide you notes of Polytechnic 5th Semester Chapter 3: INTERNET OF THINGS (IoT).
Chapter 3: Introduction to Arduino Programming and Integration of Sensors/Actuators
3.1 Introduction to Arduino Programming
What is Arduino? Arduino is an open-source electronics platform that is used to create interactive projects. It consists of both a physical programmable circuit board (microcontroller) and a software, or Integrated Development Environment (IDE), that runs on your computer, used to write and upload code to the board.
Why Use Arduino?
- User-Friendly: Easy to use for beginners.
- Versatile: Can be used for a wide variety of projects, from simple LED blinkers to complex robots.
- Cost-Effective: Affordable components and boards.
- Community Support: Large online community with plenty of tutorials and examples.
Basic Components of Arduino:
- Microcontroller: The brain of the Arduino board (e.g., ATmega328 on Arduino Uno).
- Digital and Analog Pins: Used to connect sensors and actuators. Digital pins (0-13 on Uno) are for input/output signals, while analog pins (A0-A5) read varying voltage levels.
- Power Pins: Provide power to connected components. Typically 3.3V and 5V pins.
- USB Port: Used to connect the Arduino to a computer for programming and power.
- Reset Button: Resets the code running on the Arduino.
Getting Started with Arduino Programming:
- Install the Arduino IDE: The software used to write and upload code to the Arduino board.
- Connect the Arduino Board: Use a USB cable to connect the Arduino to your computer.
- Select the Board and Port: In the Arduino IDE, select the appropriate board (e.g., Arduino Uno) and the correct port (e.g., COM3).
- Write the Code: In Arduino, code is written in sketches using C/C++ language. Each sketch has two main functions:
void setup() { }
: Runs once when the board is powered on. Used to initialize settings.void loop() { }
: Runs continuously in a loop. The main code goes here.
Basic Arduino Code Example:
"C"void setup() {
pinMode(13, OUTPUT); // Set pin 13 as an output pin
}
void loop() {
digitalWrite(13, HIGH); // Turn on the LED
delay(1000); // Wait for 1 second
digitalWrite(13, LOW); // Turn off the LED
delay(1000); // Wait for 1 second
}
- This code makes an LED connected to pin 13 blink on and off every second.
Common Functions in Arduino Programming:
pinMode(pin, mode);
: Sets a pin as input or output. Example:pinMode(7, INPUT);
digitalWrite(pin, value);
: Writes a HIGH or LOW value to a digital pin. Example:digitalWrite(8, HIGH);
digitalRead(pin);
: Reads the value from a digital pin (HIGH or LOW). Example:int buttonState = digitalRead(2);
analogRead(pin);
: Reads the value from an analog pin (0-1023). Example:int sensorValue = analogRead(A0);
analogWrite(pin, value);
: Writes an analog value (PWM signal) to a pin. Example:analogWrite(9, 128);
Debugging Arduino Code:
- Use the Serial Monitor to print messages and debug your code.
- Example:"C"
Serial.begin(9600); // Start the serial communication Serial.println("Hello, World!"); // Print message to the Serial Monitor
3.2 Integration of Sensors/Actuators to Arduino
Connecting Sensors to Arduino:
Digital Sensors:
- These sensors provide only two values: HIGH or LOW (1 or 0).
- Example: Connecting a push button.
- Connect one leg of the button to pin 2 and the other leg to the ground.
- Use a pull-up resistor if necessary to ensure a stable signal.
"C"const int buttonPin = 2; // Pin connected to the button int buttonState = 0; // Variable to store the button state void setup() { pinMode(buttonPin, INPUT); // Set pin as input Serial.begin(9600); // Start Serial Monitor } void loop() { buttonState = digitalRead(buttonPin); // Read button state Serial.println(buttonState); // Print state to Serial Monitor }
Analog Sensors:
- These sensors provide a range of values depending on the condition they are measuring.
- Example: Connecting a potentiometer.
- Connect one leg to 5V, the other to ground, and the middle pin to analog pin A0.
"C"const int potPin = A0; // Pin connected to potentiometer int potValue = 0; // Variable to store the value void setup() { Serial.begin(9600); // Start Serial Monitor } void loop() { potValue = analogRead(potPin); // Read potentiometer value Serial.println(potValue); // Print value to Serial Monitor delay(100); // Small delay }
Connecting Actuators to Arduino:
LED:
- Connect the longer leg (positive) of the LED to a digital pin (e.g., pin 9) and the shorter leg (negative) to the ground through a resistor.
- Code to control LED brightness using PWM:
"C"const int ledPin = 9; // Pin connected to LED int brightness = 0; // Brightness level int fadeAmount = 5; // Amount to change brightness void setup() { pinMode(ledPin, OUTPUT); // Set pin as output } void loop() { analogWrite(ledPin, brightness); // Set brightness brightness = brightness + fadeAmount; // Change brightness if (brightness <= 0 || brightness >= 255) { fadeAmount = -fadeAmount; // Reverse direction of fading } delay(30); // Wait for a while }
Servo Motor:
- Servos are motors that can rotate to a specific angle, typically between 0 and 180 degrees.
- Connect the power pin of the servo to 5V, the ground pin to GND, and the control pin to a digital pin (e.g., pin 9).
- Code to control a servo:
"C"#include <Servo.h> // Include servo library Servo myServo; // Create servo object const int servoPin = 9; // Pin connected to servo void setup() { myServo.attach(servoPin); // Attach servo to pin } void loop() { for (int pos = 0; pos <= 180; pos += 1) { // Move from 0 to 180 degrees myServo.write(pos); // Set position delay(15); // Wait for servo to reach position } for (int pos = 180; pos >= 0; pos -= 1) { // Move from 180 to 0 degrees myServo.write(pos); // Set position delay(15); // Wait for servo to reach position } }
Common Issues and Solutions:
Sensor/Actuator Not Working:
- Check connections and power supply.
- Verify that the correct pins are being used in the code.
Serial Monitor Not Showing Data:
- Ensure the correct baud rate is set in both code and Serial Monitor.
Code Not Uploading:
- Check if the correct board and port are selected.
- Ensure no other program is using the same port.
By understanding Arduino programming and how to integrate sensors and actuators, students can create various interactive IoT projects. For more detailed notes and updates, keep visiting the 'Rajasthan Polytechnic BTER' blog.
These notes are according to Syllabus provided by BTER (Board of Technical Education Rajasthan, Jodhpur) for Polytechnic 5th Semester Students.
Hope this post will help you. Write down Your query and Suggestion in Comment Section.
Thankyou
Regards
Garima Kanwar
0 Comments