Deep Dive into LED Scrolling Project with Arduino

LED scrolling project is one of the most popular beginner projects for Arduino. In this post, I am planning to deep dive into this project and write it again using a different way.

The purpose of this article is to show some operations inside an Arduino UNO board in order to understand working principle of a microcontroller. Hence, I will be using LED Scrolling Project as example, and working on it.

arduino ile ilgili görsel sonucu

Firstly, let us take a look at this typical Arduino project. If you are even a beginner, you can probably understand the tasks of each line. The number of LEDs used is 4 and each one blinks one time, in a back and forth formation. There are pinMode() and digitalWrite() functions to set our pins’ task (I/O) and status. There are two “for” loops that work inversely to make the code easier. Is it quite basic, isn’t it?

void setup() {

  for (int i = 2; i < 6; i++)
  {
    pinMode(i, OUTPUT);
  }
}

void loop() {

  for (int i = 2; i < 6; i++)
  {
    digitalWrite(i, HIGH);
    delay(50);
    digitalWrite(i, LOW);
  }

  for (int i = 5; i > 1; i--)
  {
    digitalWrite(i, HIGH);
    delay(50);
    digitalWrite(i, LOW);
  }
}

Also, you can see the program storage details below.
Program storage used: 990 bytes
Dynamic memory used: 9 bytes.

It works as expected and correctly. After this stage, we will change some parts of the code but before that we need to understand how it works.

In the first step of this part, we need to understand what “Arduino” is actually, what we mean when we say “Arduino”. The thing we program is not the board, is actually an AVR microcontroller called Atmega328P. Therefore, if our goal is to understand how this project works, we need to look into Atmega328P in depth, and the best way of it is using datasheet.

The pins we used for the project were pin 2, 3, 4 and 5. These pins correspond PD2, PD3, PD4 and PD5 in Atmega328P. You can see pin mapping of it below.

atmega328p pin mapping ile ilgili görsel sonucu

Then, we need to learn how to manipulate the pins. In our Arduino code, we set tasks of our pins as output or input and changed the status of them one by one. In that case, it can be said that there are two main processes in our code. Atmega328P contains two registers ,which are DDRx and PORTx, for controlling these tasks for each port. The pins that will be controlled are pins of PORTD. Thus, we need to find locations of DDRD and PORTD registers in memory.

It is time to look at the datasheet of the microcontroller. We can see the registers mentioned , and also where they are in memory.

Now, it is necessary to settle bits into these registers properly. If a bit in DDRD is high, it means that the pin with the same number will be output. In order to set pins of Port D as output, bit 2-5 of DDRD must be “HIGH”. After that, we will be changing the status of PORTD depending on which LED will light on.

We have already known the locations of the registers thanks to the datasheet. At the beginning of code, we are defining two pointers that will point to those locations.

In void setup() function, we need to set DDRD register. We are changing DDRD as mentioned above. The register will be “00111100” and the hexadecimal version of this is 0x3C.

Because of that the first LED (PD2) will light on, we need to change status according to this (as “00000100”). After this stage, HIGH bit will be scrolled in the loop section by bitwise operators.

The final version of the code can be seen below.

volatile uint8_t * const ddr_d = (uint8_t *) 0x2A;
volatile uint8_t * const port_d  = (uint8_t *) 0x2B;

void setup() {
  *ddr_d = 0x3C;
  *port_d = 0X04;
  delay(50);
}

void loop() {

  for (int i = 0; i < 3; i++)
  {
    *port_d <<= 1;
    delay(50);
  }

  for (int i = 0; i < 3; i++)
  {
    *port_d >>= 1;
    delay(50);
  }
}

You can see the storage and the difference between the previous code.
Program storage used: 682 bytes
Dynamic memory used: 9 bytes.

The difference in program storage between two versions is 308 bytes. The reason for this is that we did not use functions given by Arduino IDE.

To sum up, we saw how to control a pin of Arduino using pointers and the advantages of it. I recommend learning the background of microcontrollers to anyone with interest in embedded systems. If you make your projects with Arduino and your goal is getting a career on embedded systems, you might start learning a microcontroller in depth using different methods like in this project. Nowadays, I am working on this goal, and I would like to share posts such as this.

See you in the next post!

3 thoughts on “Deep Dive into LED Scrolling Project with Arduino

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s