Blinking LED

Blog post description.

7/2/20242 دقيقة قراءة

a close up of a board with wires attached to it
a close up of a board with wires attached to it

This is the classic "Hello World" of Arduino projects and a great introduction to basic electronics and coding.

  • Components:

    • Arduino Uno

    • LED

    • 220 ohm resistor

    • Jumper wires

    • Breadboard (optional)

  • Circuit:

    1. Connect the positive leg (longer leg) of the LED to a resistor.

    2. Connect the other leg of the resistor to pin 13 of the Arduino.

    3. Connect the short leg of the LED to ground (GND) on the Arduino.

  • Blinking LED circuit Arduino

  • Code:

C++

const int ledPin = 13; // Define LED pin void setup() { pinMode(ledPin, OUTPUT); // Set LED pin 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 }

Use code with caution.

content_copy

  • Explanation:

    • The const int ledPin = 13; line defines a constant variable ledPin and assigns it the value 13, which is the pin number connected to the LED.

    • The pinMode(ledPin, OUTPUT); line sets the pin mode of ledPin to output, which means the Arduino can control the voltage on that pin to turn the LED on or off.

    • The digitalWrite(ledPin, HIGH); line turns on the LED by setting the voltage on ledPin to high.

    • The delay(1000); line pauses the program for 1 second (1000 milliseconds).

    • The digitalWrite(ledPin, LOW); line turns off the LED by setting the voltage on ledPin to low.

    • The delay(1000); line pauses the program for another 1 second.

This code will make the LED blink on and off every second. You can change the values in the delay() functions to adjust the blinking speed.

2. Light Sensor

This project introduces the concept of sensors and how Arduino can read analog input from the environment.

  • Components:

    • Arduino Uno

    • Photoresistor (also known as a Light Dependent Resistor or LDR)

    • 10k ohm resistor

    • Jumper wires

    • Breadboard (optional)

  • Circuit:

    1. Connect one leg of the photoresistor to a 10k ohm resistor.

    2. Connect the other leg of the resistor to 5V on the Arduino.

    3. Connect the remaining leg of the photoresistor to analog pin A0 on the Arduino.

    4. Connect ground (GND) on the Arduino to the other side of the photoresistor.

Light Sensor circuit Arduino

  • Code:

C++

const int sensorPin = A0; // Define sensor pin void setup() { Serial.begin(9600); // Initialize serial communication } void loop() { int sensorValue = analogRead(sensorPin); // Read analog value from sensor pin Serial.println(sensorValue); // Print sensor value to serial monitor delay(1000); // Wait for 1 second }

content_copy

  • Explanation:

    • The const int sensorPin = A0; line defines a constant variable sensorPin and assigns it the value 0, which is the analog pin number connected to the photoresistor.

    • The Serial.begin(9600); line initializes serial communication at a baud rate of 9600. This allows you to see the sensor readings on your computer.

    • The int sensorValue = analogRead(sensorPin); line reads the analog voltage from the sensor pin and stores it in the variable sensorValue.

    • The Serial.println(sensorValue); line prints the value of sensorValue to the serial monitor.

    • The delay(1000); line pauses the program for 1 second.

When you upload this code and open the serial monitor, you will see a number that changes depending on the amount of light hitting the photoresistor. The higher the number, the brighter the light. You can use this information to control an LED or other output based on the light level.