Now that we have a basic understanding about how things fit together, let’s start with
a Hello World example.
The first thing we need would be an Arduino compatible development board and I would recommend
the Arduino UNO, if you are just starting out. An Arduino compatible board contains
the necessary hardware along with the bootloader that is required to accept code from the Arduino
IDE. You can browse the Arduino website to take a look at all of the various boards and
buy them. Or, you can simply head over to eBay or Amazon to pick up a clone which would
be much easier on your wallet. Do check the description for links to popular starter packs.
I’ll be using a custom board that I designed which is called the Piksey Nano. It is similar
to the Arduino Nano but uses a newer microcontroller – the ATmega328PB. All the programs that
we will be writing are going to be compatible with the Arduino UNO and Nano so you don’t
have to worry in case you don’t have a Piksey Nano.
Next, we need the software or the IDE. Head over to the Arduino website, download and
install the latest software for your computer. Let’s open up the IDE and set it up. Plug
in the board by using the USB cable and make sure you select the correct board and COM
port. If you have multiple COM ports on your system then disconnect the board, check which
ones are available and plug the board back in. The one that shows up after you plug in
the board is most likely the one you need to select. The COM port naming conventions
are slightly different on a Mac so keep a note of this.
The IDE contains many examples, so open up the blink example and hit the upload button.
Wait for it to upload the code and watch the LED glow. Change the delay and hit the upload
button again to see the effect. The upload button does several things behind the scenes
but it mainly compiles your sketch and uploads it to the board. If you are writing a sketch
and simply want to compile it then you can use the compile button instead. Compiling
a sketch involves many tasks but at a simplified level, it simply takes your sketch and produces
an output file consisting of 1s and 0s. This file
is then sent to the microcontroller, and the bootloader stores the program
in the correct memory locations.
Here's something to keep in mind about microcontrollers – they are incredibly dumb devices and this
will be even more evident to you once you learn about robotics. We will be getting into
robots a bit later so make sure you’re subscribed to get notified. The point here is that microcontrollers
do not have any intelligence by themselves and they execute your code blindly. If your
logic is flawed then this can give rise to inconsistencies or errors, some more obvious
than others. It’s wise that you get into the habit of thinking about all the possibilities
and sequence of events particularly when there is a user interacting with your final project.
Another thing to note is that you need to tell the microcontroller what to do at every
single moment, even if this means telling it to go to sleep. If you put a microcontroller
in a sleep state then you need to ensure it has a way of getting back into the normal
operation mode or else it would be in the sleep state indefinitely. Sleep states are
slightly advanced topics and we may cover them later on in this series. Let’s begin
by opening up and reviewing the blink example sketch.
Every Arduino sketch is made up of two functions at the very minimum – setup and loop.
The setup function is executed only once, when the microcontroller is powered ON. The
microcontroller then continuously executes the loop function, over and over again.
We generally use the setup function to carry out all the initializations and pin configurations.
Since we are interested in blinking one LED, we only need to set the correct pin as an
output. The function pinMode is used to configure the pin and you can read the Arduino reference
pages to learn more. Most Arduino boards have a build-in LED on the board connected to a
single pin. Using the keyword LED_BUILTIN tells the Arduino IDE that we are trying to
configure this pin. For the UNO and Nano, this is pin 13, and you can also replace the
keyword with 13. This keyword is also called a pre-processor
directive and it helps make your code look a bit cleaner. We will be using pre-processor
directives throughout the series.
The loop function runs over and over. We use the digitalWrite function to set the pin HIGH,
this switches ON the LED. We then use the delay function to wait for a second, and then
set the pin LOW again. We then wait for a second again. Let’s upload and test this.
As can be seen, it works as expected.
You might be wondering why do we need to call the delay functions and the reason is because
the microcontroller operates at a very high speed so it loops through the code much faster
than the human eye can keep track off. Removing the delay functions will cause the LED to
blink much faster than we can see. Try eliminating the delay function or reduce the delay to
see the effect. Keep in mind that the delay function generates a delay in milliseconds.
Let’s now try to recreate the astable multivibrator circuit we’ve seen in the previous video.
We need to control two LEDs, so let us use pins 4 and 5 and let us set them as outputs.
Here’s what the loop function will look like. We alternately SET and RESET the LEDs
while using a delay to make them visible. Let’s connect the LEDs and upload the code
to watch it in action.
Before we move to the next video, let’s learn how to read inputs. Let’s connect
a switch such that the LEDs start alternating only when the switch is pressed. We start
by configuring a pin, say pin 3, as an input. The logic for this is very straightforward:
we want the LEDs to alternate if we press the switch and that’s exactly what our sketch
will look like. Here’s how we’ve connected the switch. We have added a pull up resistor
such that pin 3 will be kept HIGH when the switch is not pressed. Pin 3 will go LOW when
the user presses the switch.
We will learn about conditional programming a little later but for now, keep in mind that
there exists something called an IF statement which can be used to create logic. Here’s
how we will be using it. We read the input pin and check if it is LOW. If the input is
indeed LOW then the IF condition will be TRUE and it will cause the statements shown here
to execute. In all other cases, the microcontroller will not do anything and it will wait for
the IF condition to be TRUE. Let’s connect the switch to the circuit and upload the sketch
to verify its operation.
Let’s create a final sketch before we move on. Let’s switch ON the RED LED when the
switch is pressed and let’s switch ON the GREEN LED in all other cases. Here’s what
the sketch will look like. When the switch is pressed, the IF condition will be true
and we switch ON the RED LED. Now, we need to ensure that the GREEN LED is also OFF so
we add a statement for that. When the IF condition is not met, then the microcontroller will
execute the ELSE statements, so we switch ON the GREEN LED and ensure that the RED LED
is OFF. Let’s upload this sketch and verify its operation.
This sketch is not exactly efficient as we constantly switch the LEDs ON and OFF which
is not really needed. We would ideally use variables or flags to keep track of the LEDs
but we’ve not done that here to keep things simple.
That’s it for this video. We will learn about serial communication in the next one
so do subscribe to stay updated. Don’t forget to like, share and comment if you liked this
video – it helps massively with the YouTube algorithm.
Thanks and I’ll see you in the next one.