157
Here are 10 handy Arduino code snippets for various common tasks:
1. Blink an LED
const int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(1000); // Wait for 1 second
digitalWrite(ledPin, LOW);
delay(1000); // Wait for 1 second
}
2. Read Analog Input
const int analogPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(analogPin);
Serial.println(sensorValue);
delay(500);
}
3. Button Controlled LED
const int buttonPin = 2;
const int ledPin = 13;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
4. PWM LED Brightness Control
const int ledPin = 9;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(ledPin, brightness);
delay(10);
}
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(ledPin, brightness);
delay(10);
}
}
5. Temperature Reading with LM35
const int tempPin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(tempPin);
float voltage = sensorValue * (5.0 / 1023.0);
float temperature = voltage * 100; // LM35 outputs 10mV per degree Celsius
Serial.print("Temperature: ");
Serial.println(temperature);
delay(1000);
}
6. Ultrasonic Sensor (HC-SR04) Distance Measurement
const int trigPin = 9;
const int echoPin = 10;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000);
}
7. Control Servo Motor
#include <Servo.h>
Servo myServo;
const int potPin = A0;
void setup() {
myServo.attach(9);
}
void loop() {
int potValue = analogRead(potPin);
int angle = map(potValue, 0, 1023, 0, 180);
myServo.write(angle);
delay(15);
}
8. RGB LED Control
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
analogWrite(redPin, 255); // Full red
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
delay(1000);
analogWrite(redPin, 0);
analogWrite(greenPin, 255); // Full green
analogWrite(bluePin, 0);
delay(1000);
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255); // Full blue
delay(1000);
}
9. Simple Debouncing for Button Input
const int buttonPin = 2;
const int ledPin = 13;
bool lastState = LOW;
bool currentState;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
currentState = digitalRead(buttonPin);
if (currentState != lastState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (currentState == HIGH) {
digitalWrite(ledPin, !digitalRead(ledPin));
}
}
lastState = currentState;
}
10. OLED Display with SSD1306
#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() {
if (!display.begin(SSD1306_I2C_ADDRESS, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Hello, OLED!");
display.display();
}
void loop() {}
These snippets are versatile and can be directly integrated or modified for your projects. Each demonstrates a practical use of Arduino functionalities, from basic GPIO manipulation to interfacing with sensors and displays.