What Are the Best Tips for Arduino® Programming?

R. Dhillon

The Arduino® microcontroller is a favorite among hobbyists, artists and robot builders who aren't necessarily electrical engineers. The Arduino® programming environment is based on Processing, a high-level language that generally is easier to learn than C++ and Java. People who are learning Arduino® programming or have been programming for a while generally will find it much easier to create functional code by going through the free Arduino® tutorials, referring to the language reference, staying organized and writing down a program's functions on paper before writing any code. Every program also should be tested before it's loaded onto the Arduino® microcontroller.

The official Arduino® website contains a large library of programming tutorials for beginners and intermediate-level programmers.
The official Arduino® website contains a large library of programming tutorials for beginners and intermediate-level programmers.

The official Arduino® website contains a large library of programming tutorials for beginners and intermediate-level programmers. These tutorials teach things such as how to light up a light-emitting diode (LED) or running a motor. Beginners should plan on going through every tutorial in the order they're presented. Compiling the code and adding comments to describe what each line means will greatly increase a beginner's understanding of Arduino® programming and improve his or her memorization of important programming concepts.

On the official Arduino® website, there are links to explanations of the various functions used by the Arduino® programming language. It also provides links to community-generated code and additional libraries that can expand the functionality of the Arduino® code and microcontroller. When a programmer comes across confusing code, forgets the Arduino® programming syntax or needs to find the name of a function, the website usually has the answers.

Organization can be the difference between functional, clear code and malfunctioning or incomprehensible code. For an Arduino® project to work, the hardware and software must complement each other. If a pin is mistakenly set as an output instead of an input in the code, the connected device might be damaged. To avoid these mistakes, make a list of all of the connected ins and outs on the Arduino® microcontroller. Write down the names of the connected devices, their pin numbers, whether they are input or output devices and their operating voltages. It is much easier to refer to a list than repeatedly looking at the connections on the Arduino®.

Staying organized also means that the code should read well. Define global variables, which are used throughout the program, before setup, and give all variables recognizable names. The variable that stores the incoming values from an ultrasonic sensor, for example, might be named "ultrasonicReading."

Variables that are used by only one function should be defined within that function. This makes it easier to debug code if a variable is storing an unexpected value. Good code also is commented well. Use comments to describe what the code is doing. This makes it much easier to spot problems, modify the code for future applications and share code with other programmers.

Small programs can be coded on the spot in the Arduino® programming application, but large programs generally cannot be. Large programs might contain many lines of code, contain multiple functions or interact with multiple devices. It generally is easier to write a large or complex program by writing down its functions in the order they occur on paper. Flowcharts are useful tools for illustrating the functionality of large programs and the scheduling of their functions.

To avoid loading bad code onto the microcontroller chip, test all code in the Arduino® programming environment first. The programming application contains a "Verify/Compile" button that looks like a typical play button. After the button is pressed, every line of code is checked for errors, including syntax errors. If an error is found, the result shows up at the bottom of the programming window.

You might also Like

Discuss this Article

Post your comments
Forgot password?