The operational principle of this circuit is straightforward, as depicted in the diagram, with the direction of current indicated. When Pin 9 emits a high-level output (5V), the LED is activated following the 220-ohm resistor that functions as a current limiter. Conversely, when Pin 9 emits a low-level output (0V), the LED is deactivated.
Step 2: WiringBuilding a circuit on a breadboard
Step 3: Code//the number of the LED pin
const int ledPin = 9;
void setup()
{
//initialize the digital pin as an output
pinMode(ledPin,OUTPUT);
}
//the loop routine runs over and over again forever
void loop()
{
digitalWrite(ledPin,HIGH);//turn the LED on
delay(500); //wait for half a second
digitalWrite(ledPin,LOW); //turn the LED off
delay(500); //wait for half a second
}
/*************************************************/
After the code is uploaded successfully, you will see the LED blinking.
Step 4: How It Works?In this setup, the LED is connected to digital pin 9. Therefore, at the beginning of the program, we should declare an integer variable named 'ledpin' and assign it the value of '9'.
const int ledPin = 9;
Now, initialize the pin in the setup() function, where you need to initialize the pin to OUTPUT mode.
void setup() {
pinMode(ledPin, OUTPUT);
}
In loop(), digitalWrite() is used to provide 5V high level signal for ledpin, which will cause voltage difference between LED pins and light LED up.
digitalWrite(ledPin, HIGH);
If the level signal is changed to LOW, the ledPin’s signal will be returned to 0 V to turn LED off.
digitalWrite(ledPin, LOW);
An interval between on and off is required to allow people to see the change, so we use a delay(1000) code to let the controller do nothing for 1000 ms.
delay(1000);
Comments
Please log in or sign up to comment.