Naan Mudhalvan
Industrial IoT and Industry 4.0
Tektork Private Limited, Coimbatore
Arduino - Basic Coding
(LED, LDR, Push Button, Potentiometer)
Tektork Private Limited, Coimbatore
Single LED Blink
const int ledPin = 33; // GPIO33 where LED is connected
void setup()
{
pinMode(ledPin, OUTPUT); // Initialize GPIO33 as output
}
void loop()
{
digitalWrite(ledPin, HIGH); // Turn LED ON
delay(1000); // Wait for 1 second
digitalWrite(ledPin, LOW); // Turn LED OFF
delay(1000); // Wait for 1 second
}
Tektork Private Limited, Coimbatore
Double LED Blink
const int ledPin1 = 33; // LED1 Connected to GPIO33
const int ledPin2 = 4; // LED2 Connected to GPIO4
void setup() {
// Initialize both GPIO pins as outputs
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
// Turn both LEDs ON
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
delay(1000); // Wait for 1 second
// Turn both LEDs OFF
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
delay(1000); // Wait for 1 second
}
Tektork Private Limited, Coimbatore
Button Serial
const int buttonPin = 32; // GPIO32 where the button is connected
int buttonState = 0; // Variable for reading the button status
void setup() {
[Link](115200); // Initialize serial communication
pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with internal pull-up
resistor
}
void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);
// Check if button is pressed (LOW because we're using INPUT_PULLUP)
if (buttonState == LOW) {
[Link]("Button pressed!");
delay(250); // Simple debounce - wait a bit to avoid multiple detections
}
// Small delay to make serial output readable
delay(10);
}
Tektork Private Limited, Coimbatore
Boot Button Serial
const int BOOT_BUTTON = 0; // Pin number for the button (using internal pull-up)
int pressCount = 0; // Variable to count number of button presses
void setup()
{
[Link](115200); // Initialize serial communication at
115200 baud
pinMode(BOOT_BUTTON, INPUT_PULLUP); // Set button pin as input with pull-up
resistor
}
void loop()
{
// Check if the button is pressed (LOW means pressed due to INPUT_PULLUP configuration)
if (digitalRead(BOOT_BUTTON) == LOW)
{
[Link]("Button pressed "); // Print button press message
delay(300); // Simple debounce delay to avoid multiple
detections from single press
while (digitalRead(BOOT_BUTTON) == LOW); // Wait until the button is released
before continuing
}
}
Tektork Private Limited, Coimbatore
Potentiometer Serial
// GPIO36 = ADC1_CH0
const int potPin = 36; // GPIO36
int potValue = 0;
void setup() {
[Link](115200); // Start serial communication at 115200 baud
delay(1000); // Small delay to ensure serial is ready
}
void loop() {
potValue = analogRead(potPin); // Read analog value from potentiometer
[Link](potValue); // Print raw value (0–4095)
delay(200); // Wait a bit before next read
}
Tektork Private Limited, Coimbatore
Button Control LED
// Pin definitions
const int buttonPin = 32; // Push button connected to GPIO32
const int ledPin = 4; // LED connected to GPIO4
// Variables
int buttonState = 0; // Variable for reading button status
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as output
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with pull-up resistor
// pinMode(buttonPin, INPUT); // Use this if you have external pull-down resistor
}
void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);
// Check if button is pressed (LOW because of pull-up)
if (buttonState == LOW) {
digitalWrite(ledPin, HIGH); // Turn LED ON
} else {
digitalWrite(ledPin, LOW); // Turn LED OFF
}
delay(500); // Small delay for stability
}
Tektork Private Limited, Coimbatore
Boot Button Control LED
const int BOOT_BUTTON = 0; // Pin number for the button (uses internal pull-up)
const int LED_PIN = 33; // Pin number for the LED
bool ledState = false; // Tracks the current state of the LED (on/off)
void setup()
{
pinMode(BOOT_BUTTON, INPUT_PULLUP); // Set button pin as input with pull-up resistor
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
}
void loop()
{
// Check if the button is pressed (LOW means pressed due to INPUT_PULLUP configuration)
if (digitalRead(BOOT_BUTTON) == LOW)
{
ledState = !ledState; // Toggle the LED state
digitalWrite(LED_PIN, ledState); // Update the LED according to the new state
delay(300); // Simple debounce delay to avoid repeated toggles
while (digitalRead(BOOT_BUTTON) == LOW); // Wait until the button is released
}
}
Tektork Private Limited, Coimbatore
Potentiometer Control LED On/Off
// Pin definitions
const int potPin = 36; // GPIO36 for potentiometer (Analog Input)
const int ledPin = 4; // GPIO4 for LED (Digital Output)
void setup() {
pinMode(ledPin, OUTPUT); // Set GPIO4 as output for LED
pinMode(potPin, INPUT); // Set GPIO36 as input for potentiometer
digitalWrite(ledPin, LOW); // Ensure LED starts off
}
void loop() {
// Read potentiometer value (0-4095 on ESP32)
int potValue = analogRead(potPin);
// Check if value is 512 or above
if (potValue >= 2047) {
digitalWrite(ledPin, HIGH); // Turn LED ON
} else {
digitalWrite(ledPin, LOW); // Turn LED OFF
}
delay(500); // Small delay for stable reading
}
Tektork Private Limited, Coimbatore
Potentiometer Control LED Brightness
// Pin definitions
const int potPin = 36; // GPIO36 for potentiometer (Analog Input)
const int ledPin = 4; // GPIO4 for LED (PWM Output)
// Variables
int potValue = 0; // Store potentiometer reading
int ledBrightness = 0; // Store LED brightness value
void setup() {
pinMode(potPin, INPUT); // Set potentiometer pin as input
pinMode(ledPin, OUTPUT); // Set LED pin as output
}
void loop() {
// Read the potentiometer value (0-4095)
potValue = analogRead(potPin);
// Map the potentiometer value to LED brightness (0-255)
ledBrightness = map(potValue, 0, 4095, 0, 255);
// Control LED brightness using PWM
analogWrite(ledPin, ledBrightness);
delay(100); // Small delay for stable reading
}
Tektork Private Limited, Coimbatore
PWM- LED Fade in/Fade out
// the number of the LED pin
const int ledPin = 4; // 16 corresponds to GPIO 4
void setup() {
// set the LED as an output
pinMode(ledPin, OUTPUT);
}
void loop(){
// increase the LED brightness
for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
// changing the LED brightness with PWM
analogWrite(ledPin, dutyCycle);
delay(15);
}
// decrease the LED brightness
for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
// changing the LED brightness with PWM
analogWrite(ledPin, dutyCycle);
delay(15);
}
}
Tektork Private Limited, Coimbatore
LED on/ Off Control Serial
const int ledPin = 33; // LED Connected to GPIO33
void setup()
{
pinMode(ledPin, OUTPUT); // Set LED pin as output
[Link](115200); // Start Serial Monitor
}
void loop()
{
if ([Link]())
{
char c = [Link]();
if (c == 'A')
{
digitalWrite(ledPin, HIGH); // Turn ON LED
[Link]("LED ON");
}
else if (c == 'B')
{
digitalWrite(ledPin, LOW); // Turn OFF LED
[Link]("LED OFF");
}
}
} Tektork Private Limited, Coimbatore
LDR Value Serial
const int ldrPin = 39; // GPIO39 (Analog input capable)
void setup()
{
pinMode(ldrPin, INPUT);
[Link](115200);
}
void loop()
{
int ldrValue = analogRead(ldrPin); // Read analog value (0-4095)
[Link]("LDR Value: ");
[Link](ldrValue);
delay(1000); // Wait 1 second
}
Tektork Private Limited, Coimbatore
LDR Dark/Bright Detection
const int ldrPin = 39; // GPIO39 (ADC1_CH3), input-only ADC pin
const int threshold = 2000; // Adjust this based on your environment
void setup() {
[Link](115200); // Start Serial Monitor
delay(1000); // Allow time for Serial to open
void loop() {
int ldrValue = analogRead(ldrPin); // Read LDR value (0–4095)
[Link]("LDR Value: ");
[Link](ldrValue);
if (ldrValue < threshold) {
[Link](" → It's DARK");
} else {
[Link](" → It's BRIGHT");
}
delay(500); // Read every 0.5 seconds
}
Tektork Private Limited, Coimbatore
LDR Control LED
const int ldrPin = 39; // LDR connected to GPIO39 (ADC1_CH3, input-only)
const int ledPin = 33; // LED connected to GPIO33
const int threshold = 2000; // Adjust this based on your lighting conditions
void setup() {
pinMode(ledPin, OUTPUT);
[Link](115200);
}
void loop() {
int ldrValue = analogRead(ldrPin); // Read LDR value
[Link](ldrValue); // Print to Serial Monitor
if (ldrValue < threshold) {
digitalWrite(ledPin, HIGH); // Turn LED ON in dark
} else {
digitalWrite(ledPin, LOW); // Turn LED OFF in bright
}
delay(500); // Small delay to stabilize readings
}
Tektork Private Limited, Coimbatore
Thank You
Tektork Private Limited, Coimbatore