web analytics
Arduino

LED with Arduino

LED with Arduino One of the most important electronic projects for beginners in Arduino controller, We will build different projects using LED such as control turn on and turn off the LED, control the time period of lighting, connect different sensors with the lighting of the LED and more.

On-Off LED using Arduino

The first project will be to control the operations (On/Off) of the LED, so we need the The following component’s to compleate all projects in this lesson:

Component Number
Arduino UNO R3 or any board1
LED – 5V different colors5
wires 2
Resistor 220-ohm 1
Mini bread-board1

Schematic Diagram

Well, we will connect the LED with the digital output D(13) from arduino board and ee will use the “digitalWrite” command here to control the on and off, when turn on we use the word HIGH and when off we use the word LOW and we will use the command delay this command to add the time period, it should be noted that the Arduino of real-time systems is accurate dealing with parts of the second, if we want to light the lamp One second we will write 1000, Connections of LED with Arduino UNO R3 it will be as follows:

Code

Make sure your connections are correct then connect Arduino with PC using USB then upload this code to Arduino Uno board or any board you own:

/* www.tocircuit.com
 Prepared by Fawzi Al-azraq 
 admin@tocircuit.com
 */
int ledPin = 13;
void setup()
{
  pinMode(ledPin, OUTPUT);
}
void loop()
{
  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(1000);
}

After uploading the code to the Arduino you will notice that the LED is on and turns off again, you can now adjust the time delay value and add more LED to build your own lighting system.


Ultrasonic Sensor with LED

We previously studied an Ultrasonic sensor and learned about the principle of its work and how we use it to measure distance, now we will use this sensor with LEDs. We will build an electronic circuit to measure the distance and print it, but we will add a LED so that if the distance is less than 25 cm will light the LED and if the distance more than that will turn off. To build this circuit you need to add a Ultrasonic Sensor HC-SR04 sensor. Connections of Ultrasonic HC-SR04 Sensor and LED with Arduino UNO R3 As follows:

Code

Make sure your connections are correct then connect Arduino with PC using USB then upload this code to Arduino Uno board or any board you own:

/* www.tocircuit.com
 Prepared by Fawzi Al-azraq 
 admin@tocircuit.com
 */

//First Defines pins numbers for the following data:
int trigPin = 13;
int echoPin = 12 ;
int led = 11 ;

// Now defines variables
long duration ;
int distance ;

void setup()

{

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
Serial.begin (9600);

}

void loop()
{ 

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance = duration*0.034/2;

if (distance < 25)

{ digitalWrite(led,HIGH);

}

else {

digitalWrite(led,LOW);

}
// Prints the distance on the Serial Monitor
Serial.print(distance);
Serial.println("Distance in Centimeter");

delay(750);

}

Now you can move objects in front of the sensor and monitor the lighting of the LED and read the values of distances from the serial monitor, you can modify the code, for example, you can increase the distance or add more LEDs.

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *

Close
Close

Adblock Detected

Please consider supporting us by disabling your ad blocker