Skip to main content

Arduino Blinking LEDs

Arduino boards have I/Os (Input/Output) that can either receive or transmit signals. One of its basic functions is digital input/output. It can be programmed to output digital signals that can be used drive devices, such as an LED.

To drive an LED, you will have to add a resistor to regulate the current to the LED. The digital output is about 3V (5V or a little less for 5V board), and for an LED the forward voltage is about 2V. What is left (1V) divided by the resistor sets the current. Current is typically 10 to 20mA.



Driving an LED




Below is an example code for alternating LEDs on Arduino Pro Micro Board. It uses pins 2, 3, 4, and 5 as digital outputs. My 11 year old son helped out in the programming. :)

void setup() {
  // initialize digital pins 2, 3, 4, and 5 as outputs.
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(2, LOW);   // turn the LED off (LOW is the voltage level)
  digitalWrite(3, LOW);  
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
 
  delay(500);
 
  digitalWrite(2, LOW);  
  digitalWrite(3, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);
 
  delay(500);
  
  digitalWrite(2, LOW);  
  digitalWrite(3, HIGH); 
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);
 
  delay(500);
   
  digitalWrite(2, HIGH);  
  digitalWrite(3, LOW);  
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH); 
 
  delay(500);
  
  digitalWrite(2, HIGH);  
  digitalWrite(3, HIGH);  
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH); 

  delay(500);
 
  digitalWrite(2, LOW);  
  digitalWrite(3, HIGH);  
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);

  delay(500);
 
  digitalWrite(2, HIGH);  
  digitalWrite(3, LOW);  
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
 
  delay(500);
 
  digitalWrite(2, HIGH);  
  digitalWrite(3, HIGH);  
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);
 
  delay(500);
 
  digitalWrite(2, HIGH);  
  digitalWrite(3, HIGH);  
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);
 
   delay(500);
  
  digitalWrite(2, HIGH);  
  digitalWrite(3, HIGH);  
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
 
  delay(500);

  digitalWrite(2, HIGH);  
  digitalWrite(3, LOW);  
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);

  delay(500);
 
  digitalWrite(2, LOW);  
  digitalWrite(3, LOW);  
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
 
  delay(1000);              // wait for a second
}






Comments

  1. Please accept my deepest gratitude for your outstanding effort. Your writing is flawless, and it is genuinely amazing how well you are able to communicate difficult concepts. Reading your work has been a real pleasure for me, and I've learned a lot and developed some new perspectives.
    if you want to buy robotic components then kindly click here.Arduino Boards

    ReplyDelete

Post a Comment

Popular posts from this blog

Arduino Transistor DC Motor Control

Arduino boards have PWM (Pulse Width Modulation) outputs that can be used to control like the speed of a DC motor. On a Pro Micro, those outputs are encircled white on the board. You can program it such that it outputs on a scale of 1-255, 1 being the slowest and 255 being the fastest. PWM gives out pulses whose width is varied (modulated) while the period is constant. The longer it is high, the higher power it delivers to the circuit. PWM can be used to drive a transistor (switch) which in turn drives the motor ON and OFF. The longer the switch is ON, the higher the power delivered to the motor, and thus faster. The circuit consists of a transistor Q1 (TIP31) NPN transistor, driven by the PWM from Arduino. The resistor is to limit the current to the base, but enough to operate the transistor in saturation when the input is high. The diode is to protect the motor from any back emf that might come from the motor when the current is cut off. The circuit is supplied by a 9V battery. ...

Resistance, Capacitance, and Inductance (R-C and R-L Circuit)

Our aim here is to discuss the very important concepts and give intuitive explanations of the subjects. We'll start with the resistor which is the most straightforward, then with the less understood and often misunderstood concepts of capacitance and inductance. We will discuss the R-C and R-L circuits as well. These along with Ohm's Law, Kirchoff's Voltage Law (KVL) and Kirchoff's Current Law (KCL) are the very foundations of understanding the complete behaviors of practically any circuit one might encounter. Resistance It is a general term that refers to the opposition to the free flow of current in a circuit. More accurately, it refers or applies to DC (direct current). Resistance doesn't apply only to resistors but also to a host of other components in the circuit. For example, a copper trace has a resistance according to its dimensions, as well as the leads of he components.When a current flows through a resistor a potential difference is generated acr...

Practical Integrator Circuit - A Triangle Waveform Generator

Below is a basic op amp configuration of converting a square wave input to triangular waveform. Figure 1 The concept is pretty simple. The square wave input produces a current that is translated to a voltage ramp across the capacitor. The positive cycle of the input produces a negative ramp output, while the negative input produces a positive ramp. The ramp is governed by the following basic equation: Equation 1 where Ic  is produced by the input voltage divided by Rin, since the same current flows in the resistor and in the capacitor. Note that the input has to be centered and symmetrical about the positive input terminal which is connected to ground, in order to produce a symmetrical output. But this basic configuration has some serious problems. Even if the output starts at zero, it will not be centered at zero, and will not stay at the same center or common mode value due to the inherent offsets of the amplifier. Figure 2 A technique in order to hold th...