Click here to Skip to main content
13,831,956 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 14 Jun 2018
Licenced CPOL

Connecting an Arduino to a Breadboard to Light Up LEDs

, 14 Jun 2018
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "Connecting an Arduino to a Breadboard to light up LEDs"


This is short write-up inspired by a great Connecting an Arduino to a Breadboard to light up LEDs article published as part of Summer Fun with Arduino Challenge. While taking this challenge and analyzing the source code, I realized that the code can be significantly simplified if we write the full byte into the output port at once, instead of setting individual bits in a loop. If we adopt this approach, we won't need to maintain an array of pin numbers and manipulate this array in the code.


If we connect our 8 LEDs to the first 8 digital pins (pins 0 through 7 aka Port D), then writing a code of 1 (binary 00000001) to the port will light up LED 0 (the rightmost). Writing code 2 (binary 00000010) will turn LED 1 on and LED 0 off. Code 3 (binary 00000011) will turn both LED 1 and LED 0 on and so on, until code 255 (binary 11111111) will turn all LEDs on. This is a simple way to control all LEDs at once rather than setting them individually.

Connecting to the Device

I have made minor changes to the original connection diagram by using individual resistors for each LED. All cathodes (short legs) of LEDs sit on a negative bus connected to Arduino board GND pin. Each anode (long leg) is connected to a resistor and that resistor is connected to the corresponding digital pin of Port D.

The whole system looks like this:

Using the Code

The code now looks very simple. It's a plain C which reads like JavaScript.

We define a byte variable counter which gets incremented every 100 msec. In the setup() method, we configure all pins of Port D as output. In the loop() method, we send current value of the counter to the port and increment it in one statement. Once the counter reaches its max value (255), it resets to 0 automatically.

byte counter = 0;
// Initial setup
void setup() {                
  DDRD = 0xff; // set all pins of Port D to output
  PORTD = 0;   // turn all LEDs off
// Infinite loop
void loop() {
    PORTD = counter++; // write current counter value to the port and increment the counter

And that's it. Happy coding!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Leonid Fofanov
Web Developer
United States United States
In my last 20+ years I was building mostly web applications using C#/ASP.NET, Sitefinity, SharePoint, J2EE, Classic ASP and ColdFusion. Earlier I spent several years coding for embedded systems.

You may also be interested in...

Comments and Discussions

QuestionGood projet Pin
Member 124464206-Jul-18 6:13
professionalMember 124464206-Jul-18 6:13 
AnswerRe: Good projet Pin
Leonid Fofanov11-Jul-18 16:57
memberLeonid Fofanov11-Jul-18 16:57 
GeneralMy vote of 5 Pin
Richard MacCutchan2-Jul-18 5:47
protectorRichard MacCutchan2-Jul-18 5:47 
GeneralMy vote of 5 Pin
Member 1069926127-Jun-18 12:26
memberMember 1069926127-Jun-18 12:26 
PraiseExcellent article Pin
Member 1374358416-Jun-18 2:15
memberMember 1374358416-Jun-18 2:15 
GeneralMy vote of 5 Pin
raddevus15-Jun-18 3:24
mvaraddevus15-Jun-18 3:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190114.1 | Last Updated 14 Jun 2018
Article Copyright 2018 by Leonid Fofanov
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid