.st0{fill:#FFFFFF;}

Arduino

Arduino – A dive into the fundamentals of the blinking LED 

 January 31, 2018

By  Peter

Join Our Mailing List

We publish fresh content each week. Read how-to's on Arduino, ESP32, KiCad, Node-RED, drones and more. Listen to interviews. Learn about new tech with our comprehensive reviews. Get discount offers for our courses and books. Interact with our community.

One email per week, no spam, unsubscribe at any time.

The majority of Arduino buyers purchase an Arduino kit that contains an Arduino circuit board and a few hardware parts to test and examine. The directions that come with the kit show you how to test the parts. Many people don’t like directions, but if you struggle through the directions by pulling a little hair out of your head, almost anyone can do it. The most important part is, do you understand what you just did. If you ever want to make the Arduino do something that you want it to do, you need to understand the procedures that came with your kit. The directions are good at showing you how to hook up circuits and how to test them, but how it all works, the directions are not so good.

This is a supplement to the directions that shows how it all works. I choose the blinking LED to explain. After understanding that, a person should be able to figure out the rest of the projects.

This is the block diagram for the Arduino microcontroller that is plugged into your socket of your Arduino circuit board. The purpose of this whole write up is to show you how the first project circuit works on the Arduino. That first circuit project is the blinking LED.

Instead of the Arduino testing the LED, we could test that circuit with a switch, a battery, a resistor (the resistor is used to limit current so the LED does not burn out) and a LED. We could manually turn on and turn off the switch every second and the whole circuit would be tested. We could build a computer system that could do the same thing. That computer system would contain very few parts and the software would be very small. That computer system could be explained in 5 minutes. Computers were originally designed to do many hundreds of different things. They are now designed to do many thousands of things. That has made the hardware and the software more complex. But the basic design of a computer is still the same and is simple. If you always keep that fact in the back of your mind, you will never be totally confused with computers.

Computers work with instructions. Each individual instruction performs a particular task. You can perform more a complicated task by combining different instructions together in unique combinations. The instructions that are put together for the arduino blinking sketch are saved in flash memory. When the blinking program is executed, the CPU removes a copy of each program instruction from flash memory and executes them one by one until it get to the end of the program. In case of the blinking LED program there is a jump back instruction that loops the software back to the start, so it can do it over and over.

A standard computer system is broken down into two parts, a CPU and a motherboard. The CPU controls the mother board with the address bus and the data bus. The CPU address buss does two things, it chooses address numbers on mother board memories and controls the decoder that turns on each motherboard circuit. Some of circuits that the motherboard decoder turns on is: the Ram, the Rom, the Display circuit, the USB circuit, the input circuit and the output circuit. The data bus is a two way bus that can send data numbers or receive data numbers. Some of the circuits and parts that are connected to the data bus are: the Ram, the USB circuit, the input circuit, the output circuit, and the Rom. Note: The address bus is an important part of a computer system, it is missing on the Arduino block diagram. I think they did this to save space.

The Arduino is a microcontroller. A microcontroller has CPU parts and motherboard parts in the same integrated circuit.

Just like a standard computer, the microcontroller has a CPU. The other parts in the microcontroller are:

  1. Flash memory – Used for program memory, sketch memory, library memory and boot loader memory.
  2. SRAM – Used for storing the variables in program software, sketch software, and library software.
  3. EEPROM – Used for remembering data and variables when power is turned off.
  4. Serial peripheral interface – the SPI bus is a digital serial bus that has 4 wires. The 4 wires are connected to 4 Arduino pins. One wire is serial data, another wire output is a clock for that data. And the last 2 wires are control signals. On one of the sketches the SPI bus is connected to an external shift register that changes the serial data to parallel data. The parallel data turns on 4 LEDS.
  5. Two wire interface – the TWI (I 2 C) bus very similar to the SPI bus. The bus can read or write like the SPI bus and has a data signal and a clock signal. But this bus is slower than the SPI bus and only has 2 wires. The 2 wires are connected to analog pin 4 and 5.
  6. Port D – is an 8 pin digital input or output port. All the ports are connected to Arduino pins.
  7. Port B – is an 8 pin digital input or output port. Two of the pins on the Arduino are connected to the SPI bus.
  8. Port C – is a 6 pin analog input port. An analog to digital converter converts an input pin voltage into a digital number. The CPU then reads the digital number and converts it to a voltage. Analog input Pin 0 and pin1 also go to the UART bus.
  9. Analog compare – is a circuit that monitors analog voltage on Port C. When it gets to a predetermined value set by software, it turns on a flag that says it found that value.
  10. Watch dog – is a counter circuit that is trying to down count to zero. If it gets to zero, the CPU resets. Running programs periodically load the counter to its highest number, so the counter never gets to zero. If software gets lost, the load signal goes away and the counter down counts to zero, which resets the CPU. The other reset circuits on the Arduino are, a momentary reset when you turn power on, and a hard reset when power voltage gets too low.
  11. UART – the universal asynchronous receiver transmitter is the part that receives and transmits data from the USB Arduino cable. The 4 wires in the USB cable are for power, ground, transmit, and receive.
  12. Counters – There are two 8 bit counters and one 16 bit counter. The normal way to count time is to count up to a certain number, and that will equal a certain amount of time. A computer can count a number, but the counting would be too fast and the number used would be too big. Big numbers would slow down the computer. The hardware counters in the Arduino count slow to fix those problems.

We are now going to see how the Arduino works step by step. I am not going to hook up wires or download libraries. I am assuming that you already know how to do that. Almost all the concepts and principles that apply to the blinking LED, will also apply to the other sketches.

Before I start I want to explain serial and parallel number transmission. Lets say I have 3 tennis balls. Each ball had the letter of my name on it. J ball, I ball, and M ball. They are all side by side on the left side of a table and they spell JIM. If I wanted to transfer the 3 balls to the right side of the table, I could have 3 grooved channels that are elevated on one side. If I started all 3 balls rolling on the elevated side, all 3 balls would roll to the other side of the table. JIM would be transferred from the left side of the table to the right side. All the 3 balls rolling across 3 channels are called parallel transmission. If I only used one grooved channel and put each of the 3 balls on the elevated side, the 3 balls would roll across the table. JIM would be transferred from the left side of the table to the right side. The 3 balls rolling across only 1 channel is called serial transmission.

Computer hardware only works with binary numbers. Binary numbers are a series of 1(ones) and 0(zeros). That means the tennis balls would be a 1 or 0. Binary numbers are transferred with wires. One wire plus a clock could be used for serial data transmission and 8, 16, 32, or 64 wires would be for parallel transmission.

This is what happens when you up load the blinking sketch.

  1. The sketch gets turned into a C + + program
  2. The compiler called avr-gcc turns the C + + human readable code into a code that the computer can read. That code is called machine code (object files) which contains binary number instructions and data .
  3. That code gets combined with (linked) the standard Arduino libraries that provide basic functions like, digitalwrite() or serial.print ().
  4. The end result is a single hex file that is the blinking LED program.
  5. That hex file is transmitted over the USB cable and put into Arduino flash memory. Flash memory is contained inside the Arduino microcontroller chip.

Assumptions:

Before explaining how the blinking code works on the Arduino a few assumptions need to be made. Most of the programming code that is needed for the blinking LED is not shown on the arduino sketch. It could take years to understand all the code that goes into making the LED blink. Software today is not written for one purpose only. If your computer software was designed to only turn on one LED, the software would be very small. The software today is designed to do many different projects. There is common software that every project uses. An example of that is input and output. There is no reason to write new input and output software when it has been done thousands of times before by other people. Don’t write your own, use the standard input and output library. You might need to modify it a little by defining an output pin, but that is easy. When programmers are working on a new project they will always try to use a combination of many different software programs that have already been made.

You need to keep this fact in mind when you write and execute C instructions on the Arduino. Each C instruction must be converted to a binary number or a few binary numbers. The reason for that is, wires are used to move information around in a computer. The binary number system is the perfect number system to be used with wires.

When a line of C code has a “;” symbol on the end, it is a statement. A statement can be a combination of functions, parameters, integers and C code.

A C code with the “{” symbol means the beginning of a programming routine. When you see the “}” symbol it is the end of a programming routine.

Here is the sketch code for the blinking LED program on the Arduino.

Int LED 9;

void setup() {
pinMode(LED, OUTPUT);
}

void loop() {
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
}

I will now explain how every part of the code works.

The first line starts with – “int led = 9;”. Int is a number without a fraction. Led is the name of a variable that is defined anywhere in your sketch. 9 is what is stored in that variable. Arduino’s SRAM is used to store variables. You can change the value of a variable in S Ram at any time you want in your program.

The second line starts with – “void setup()” which is the setup function. The C code symbol “{” starts “pinmode”. Pinmode is a sub function of setup. The 2 parameters of “pinmode” are – “LED” and “OUTPUT”. Those 2 parameters select pin 9 of the Arduino to be an output. The code symbol “;” ends that statement. The code symbol “}” ends setup.

The next software routine is “void” loop. It contains the “loop” function. The code symbol “{” starts “digitalWrite”. “digitalWrite” is a sub function of “void” loop. The 2 parameters of “digitalWrite” are “LED” and “HIGH”. They select pin 9 of the Arduino to output a high. That high turns on the LED that you built on your circuit board. The C code symbol “;” ends that statement routine.

The next software routine function is the delay function- “delay(1000);”. That routine down counts a hardware counter in the microcontroller so the LED will stay on for one second. The code symbol “;” ends that statement.

Next, the “digitalWrite” function starts again. The 2 sub functions of “digitalWrite” are – “LED” and “LOW” – they select pin 9 of the Arduino to output a low. That low turns off your LED. C code symbol “;” ends that statement.

The delay function is accessed again – “delay(1000)”. That routine leaves the LED off for one second. C symbol “;” ends that statement.

The last code symbol “}” ends the code and “void” loop starts again.

Written by James Fern Buchanan


Tags

Arduino, Blinking, Led


You may also like

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

For innovators and hobbyists looking to unleash the full potential of their Arduino projects, delving into the realm of wireless technology is essential. Arduino, the compact microcontroller that has revolutionized DIY inventions, can truly flourish

Read More
Exploring Wireless Options for Arduino Projects

I’m excited to introduce you to the latest and greatest version of KiCad – version 8 (stable release). This article will overview the new features and capabilities added to this popular open-source electronic design automation

Read More
KiCad 8: The new and updated features, a full review