We’re continuing with our “Arduino for Absolute Beginners” series, which has so far covered the installation and testing of a Nano board (HERE), as well as the major components of the Arduino IDE and an introduction to using the Serial Monitor for debugging (HERE). This entry covers important syntax, introduces functions and re-usability of code, and we start working with breadboards. ALSO! I asked my buddy Cola from eMakeFun if we could have a coupon for a component pack which included a breadboard and he delivered! We’ll un-box the eMakeFun Electronic Components Start Kit.
Here’s a parts list of components used in this blog:
eMakeFun Electronic Components Starter Kit 20% Coupon – JVJRAPR9
eMakeFun Arduino Nano 5 pack 20% Off Coupon – 8H4NTIYN
For those of you who want practice soldering or are teaching a young engineer how to solder, here’s a version of the Nano pack where you can solder the headers on yourself:
eMakeFun Nano 5 pack – un-soldered 20% Off Coupon – CCUOJVPY
Please note that these are affiliate links, so, if you order from them the Smarter Home Club does get a very small percentage of the order.
CASE, Semiconlons, Parentheses, Brackets, and Comments – Important Syntax information
The absolutely most important concept for a beginner to learn in Arduino programming is that C++ is CASE SENSITIVE. These two lines have different meanings to the compiler and only the first of them will set the baud rate for serial transmissions in Arduino.
The next critical concept to learn is when to use a SEMICOLON. The short answer is that you need to use a semi-colon to indicate the end of a statement.
In more detailed terms – you have operands (Variable A, Variable B) which are acted upon by operators (+, -, *, /, etc). Expressions combine operators and operands to perform a calculation or assign a value. Example – Variable A * 10 = Variable B is an expression. A statement uses one or more expressions to form the basic programmatic structure.
That is still a dramatic over-simplification of the involved terms, but you now have a basis to continue.
When you combine operands and operators into an expression, you can enclose the expression in PARENTHESES (you do not have to). This can make it a lot easier to follow the code you’ve written and can also affect the order in which calculations are executed. Example:
VariableA + VariableB * VariableC
(VariableA + VariableB) * VariableC
In the first formula, the compiler executes VariableB * VariableC before adding VariableA, where as in the second example the parentheses indicate to the compiler that VariableA should be added to VariableB prior to multiplication by VariableC. If you remember PEMDAS from school, “Please Excuse My Dear Aunt Sally,” it instructs that the order of operations is Parentheses, Exponents, Multiplication/Division, Addition/Subtraction. Arduino will follow this in the execution of your expressions.
You should also note that there is a set of parentheses at the declaration or invocation of any function. If there are variables to pass, these are defined in the parentheses at the functions declaration. The values are passed in parentheses when the function is invoked.
Speaking of functions – when you write a function or you execute a loop, use brackets to enclose them. Brackets enclose (mark the start and end of) any logical block of code. For example:
This code snippet illustrates enclosing an expression in parentheses (in this case the evaluation of the variable sensorVal). It encloses the If statement inside curly brackets. It calls the digitalWrite function and passes the required variables, pin number and state, in parenthesis, and it ends each statement with a semicolon.
Another important concept is comments. A good rule of thumb is that when you come back to your code after a year, your comments should make the code as easy to understand as it was when you finished writing it. Admittedly, Arduino code is usually limited in scope compared to other programming projects, but it is a good habit to learn now.
There are two ways to comment code in Arduino – single line and multi-line. One sentence and many sentences.
While you’re a beginner, comment EVERYTHING. It doesn’t matter that you comment too much. It doesn’t matter if you have 5 times as many lines of comments as lines of code. Over time, you’ll come back to old code, realize and learn what is important, and refine your commenting practices over time. You’ll never kick yourself for writing too many comments, but you will if you don’t write enough.
Another very important concept is the re-usability of code. You can write your sketch however you want. If you focus on certain practices regarding functions and code re-usability, you’ll save a lot of extra effort. Any time there is a piece of code you’re going to use several times, offload it to a function. This will make your sketch shorter (which uses less memory and runs faster), will make your code easier to read, and can prevent bugs by default.
When you work with more advanced projects, it becomes even more important, as you start to differentiate between “synchronous” and “asynchronous,” or non-synchronous, code. You’ll use callbacks (where you “call” a function while something else is running) to execute a set of instructions and then terminate, so that you don’t interrupt the “something else” that is running. By maximizing your use of functions now, you’ll already have an “event driven” mindset when you start working with asynchronous code.
As we continue in the tutorials, I’ll make a point of writing out as much code as possible into functions and pointing it out when I do, so that you see what is happening. Starting with the next tutorial, every entry in the series will have a specific project in mind to teach a given subject, rather than general introductions (like the first three entries have done).
Speaking of introductions, the last item to introduce to you here is the breadboard. Your breadboard is what makes your Arduino more than just a blinking light. Your Arduino Nano clone board has 8 analog and 14 digital pins (digital pins include TX, RX, and 6 PWM pins). It connects to your PC via a Mini-B USB port, which also provides power to the board. You don’t have to power the Nano via your USB. It can accept 5v regulated DC, which means with a breadboard and power supply, your Nano is portable. Even without some form of network connectivity, that makes a Nano board a powerful tool for learning and experimentation. the eMakeFun Component kit just happens to have a breadboard compatible power supply in it. Let’s take a look at the un-boxed components:
12 different resistor values (including a card with a color code chart to help you identify them), 7 different capacitor values (Electrolytic, ceramic, metal, monolothic types), LEDs, potentiometers, buzzers, relay, header pins, jumper wires, a 20 pin male to female Dupont style wire, a 9v adapter for the power supply, the power supply, your breadboard, and more. That’s a lot of loot in one kit.
NOTE: DON’T PLUG YOUR NANO INTO THE BREADBOARD WHEN POWER IS APPLIED TO THE NANO OR THE BREADBOARD.
The breadboard has two sections of pins on the left and right side, labeled + and -, allowing you to connect a power source to the board, like a transformer, battery, or a bench power supply, and then connect components to those two rows. These sections are actually detachable and are isolated from the rows of pins on each side. You connect them via jumpers. Note that there also is a center channel in the breadboard – this isolates the two header rows on your Nano from each other.
One of the reasons why I picked the Nano to start with is because it is “breadboard friendly.” What that means is that it is narrow enough, from one row of header pins to the other, that it can fit on a standard breadboard and still have room for pins on each side. With the Nano, that’s how you connect components to the board’s pins – via a jumper wire on the breadboard – so you’ve got to have space on the same horizontal row as the pin in order to connect.
This is an ESP32 dev kit. Notice that it has no exposed pins on either side when plugged into a standard breadboard. There’s a way to get around this.
I took two 400 pin breadboards and removed a power rail from one. I then connected the two boards by inserting the pegs from one board to the notches on the other. You’ll also notice that I’ve jumped the positive and negative rows on all three power rails.
Some boards provide full rows that you can power from a single source. Others break up the rows into two, which is handy for isolating some projects, but, for the ESP32 board, I wanted the full length available. As you see in the second picture above, there’s plenty of access to pins on both sides of the board.
Whenever you’re working with breadboard components, check the pins before you plug it in. Bent pins like these can end up broken if you don’t examine them and straighten any bent ones first.
Once the pins are straight and the power adapter is plugged into the breadboard. For the Nano, we want to provide 5v DC. However, there are boards that require 3.3v. There are times when you’ll want to use both voltages at the same time. Having an independent jumper for each rail is a nice touch.
Here’s the complete setup – Nano, connected to 5v via the power supply, which, in turn is connected to a 9v battery via the adapter included in the component kit. Now, whether we use the PC to connect or want to do something portable, we’re ready to learn about electronic circuits as well as learning how to program the Arduino Nano!
Next time, we’ll start some real coding as we work with resistors!
SmarterHome.club is the website for our Facebook community, The Smarter Home Club – which is an umbrella for all kinds of smart home technologies – home automation, security, custom electronics, weather stations, alternative energy, you name it. DIY focused.
If you’re interested in joining the Smarter Home Club’s Facebook group, please follow this link: