0% found this document useful (0 votes)
3 views

Arduino_Sensors_Examples

This document provides an overview of various Arduino sensors and modules, including LCD, OLED displays, servo motors, RGB LEDs, and several sensors like ultrasonic, temperature, gas, and motion sensors, along with example code snippets for each. Each section explains the functionality of the component and includes code to demonstrate its use. The document serves as a practical guide for implementing these components in Arduino projects.

Uploaded by

badkiaan123
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Arduino_Sensors_Examples

This document provides an overview of various Arduino sensors and modules, including LCD, OLED displays, servo motors, RGB LEDs, and several sensors like ultrasonic, temperature, gas, and motion sensors, along with example code snippets for each. Each section explains the functionality of the component and includes code to demonstrate its use. The document serves as a practical guide for implementing these components in Arduino projects.

Uploaded by

badkiaan123
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Arduino Sensors & Modules: Explanation & Code Examples

This document provides an overview of commonly used sensors and modules with Arduino,
along with example codes. Each section includes an explanation followed by the
corresponding code snippet.

LCD (16x2) Display


A Liquid Crystal Display (LCD) is used to show text and numerical values. The following
code initializes the LCD and prints 'Hello, Arduino!'.

Example Code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
lcd.begin(16, 2);
lcd.print("Hello, Arduino!");
}

void loop() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
delay(1000);
}

OLED Display
An OLED display is a graphical screen that can display text and images. The following code
initializes an SSD1306 OLED display and prints 'Hello OLED!'.

Example Code:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128


#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.println("Hello OLED!");
display.display();
}

void loop() {}

Servo Motor
A servo motor is used for precise control of angular position. The following code moves the
servo to 0°, 90°, and 180° positions in a loop.

Example Code:

#include <Servo.h>
Servo myServo;

void setup() {
myServo.attach(9);
}

void loop() {
myServo.write(0);
delay(1000);
myServo.write(90);
delay(1000);
myServo.write(180);
delay(1000);
}

RGB LED
An RGB LED can display different colors by adjusting the intensity of the red, green, and
blue channels. The following code cycles through red, green, and blue.

Example Code:

int redPin = 9, greenPin = 10, bluePin = 11;


void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}

void loop() {
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
delay(1000);

analogWrite(redPin, 0);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
delay(1000);

analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
delay(1000);
}

Ultrasonic Sensor (HC-SR04)


The HC-SR04 sensor measures distance by sending ultrasonic waves. The following code
reads the distance and prints it to the Serial Monitor.

Temperature Sensor (DHT11)


The DHT11 sensor measures temperature and humidity. The following code reads values
and displays them on the Serial Monitor.

Gas Sensor (MQ-2)


The MQ-2 gas sensor detects gases like methane, butane, LPG, and smoke. The following
code reads gas levels and displays them.

Motion Sensor (PIR)


A Passive Infrared (PIR) sensor detects motion. The following code turns on an LED when
motion is detected.

You might also like