Arduino_Sensors_Examples
Arduino_Sensors_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.
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>
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:
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);
}