Hello Arduino
arduino Arduino UNO Estimated reading time: 10 minutesWe all use software that can use hardware capabilities and convert our actions into the desired result. But do we understand how everything works on the hardware level? How software code translated into 0 and 1 and how electric current produce magic for us?
I always wondering how things work under the hood. And in my opinion, to say that u understand how something works, u need to be able to create the same thing from scratch. I know - for some object it may require years to get all this knowledge, but we have no other way.
Arduino
One of the best ways to start - is to start from something simple and increase complexity step by step. I decided to start from Arduino. Arduino is an open-source electronics platform based on easy-to-use hardware and software.
This platform allows u to start easy - u just need basic knowledge of c++ and pc with Arduino IDE.
Setup
To setup Arduino and start the experiment we need a few things:
- coding environment
- debugging
- hardware
Coding Environment
As I mention above, Arduino provides its IDE that is quite simple and has only a few buttons.
This is great and simple, we may use it for a really fast start. U need just to install it, connect the board via USB and u already should be able to run the program. But at the same moment simplicity bring some limitations:
- no autocomplete
- no build-in documentation
- no possibility to debug
- hard to manage a few source files in projects.
I always prefer to check alternatives before a final decision - here is not an exception. One of the alternative tools - VisualCode vs plugin for C++ and Arduino. This combination brings to us a few advantages:
- autocomplete
- possibility to review documentation (if exist in the source)
- predefined set of commands available from CommandPallete
Such a combination still has some limitations, but a bit less than with Arduino IDE.
Few tips:
- To create project - use command from CommandPallete
Arduino: Initialize
This command will create a project and add additional settings for the project - hidden folder .vscode
, where files arduino.json and c_cpp_properties.json are located. In these 2 files, u can setup a configuration for the project.
Check this link about configuration for more
If u like CLI this link also could be useful for u
arduino.json contains all Arduino-specific settings for a project. (alternative for this is CommandPallet actions or bottom panel (described below)).
This is my c_cpp_properties.json:
The most interesting part is includePath
- contains all the libraries needed.
Alternative option - just configure everything from the bottom status panel in Visual code (effect - same as if it configured in arduino.json).
U should select the next items:
- select language - C++,
- select Programmer - the way how your program maybe upload to the device (depend on programmer u have, u may select AVR ISP or left it unselected for USB port)
- set file with fw (if u use a command from CommandPallete than it will be selected automatically. To change - change
sketch
value in arduino.json) - select board at which u would like to upload fw
- next button with port action - connect/disconnect to the serial port
- and select port which uses for communication
Next few tips:
- To send value over serial port -
Arduino: Send text to serial port
- To change baudRate -
Arduino: Change baudrate
To successfully use Serial port make sure u set the correct baudRate in both program and settings.
- Upload and check code action available from the top right corner of Visual Code
If u like to upload or check code using Arduino IDE, bud write code in some other environment - u may need to select checkmark in setting Use External Editor
- In Arduino IDE, select File -> Preferences -> Check “Use External Editor”
- In VS Code, select the folder you are working on and just code as usual
- If you are ready to upload your code, go back to Arduino IDE and press the upload button
- to support few files within a project (one
.ino
and few c++ classes) u need to be sure, that a folder has the same name as the.ino
file. Then just position addition.h
and.cpp
files in the same folder.
Debuging
Unfortunately debug Arduino is not as easy as u might think. Firstly it’s because of simple IDE - the toolset is minimal and debugging is not included. But we all know a lot of different techniques for debugging - from visual inspection to memory dump analysis.
As for me (at least for now, and at the moment of writing this, I’m not very experience in Arduino), one of the best options is to combine a few practices:
- hardware check
- code analyze
Serial
port output (logging)- external tools for debugging (simulators)
Hardware check
This is the very first step that needs to be done. U need to be sure that everything is correctly configured - wires connected properly, components are positioned correctly (check polarity), power system connected correctly.
This small step may prevent half of the problems.
Code analyze
This feature has already been added to IDE and its essential part that allow simply to avoid any typo and lexical errors. Works great within both Arduino IDE and Visual Code.
We also can use the Validate option to compile and to analyze the errors.
As David Thomas and Andrew Hunt mentioned in their book* “The Pragmatic Programmer” - *Read the Damn Error message. It’s cool advice, especially when u have to deal with Arduino ;].
U can also check your code manually to make sure that logic is correct.
Some people can propose to add comments into code. I prefer to avoid them because as soon as u add them into the code, they become obsolete. Better make your code tell the story for others, not the comments. Comments for me - an indicator of workarounds and some unresolved, known bugs.
Serial
port output (logging)
This kind of live-session debugging way. U simply add log output and inspect state and variables when something is executed.
External tools for debugging (simulators)
There are a bunch of them, like Virtual Breadboard. I checked a few and one of the coolest is Circuits.io. This tool allows you to design, prepare instruction, and generate basic code for u.
Hardware
Hardware u can get by visiting the official site of Arduino and by ordering any set of parts. I started from Arduino Starter Kit Uno.
This kit also includes a perfect guide, which as an option available in pdf.
My first app
I started with a simple classic app - colored diode. My target - is to assemble a scheme that allows me to change the color of the diode with some command.
First of all - components. For this board I just needed a few of them:
- 3-color diode (RGB LED Common Anode)
- Arduino UNO vs power cord
- few resistors (diode should be protected)
- some wires to connect
Assembled scheme:
The program idea - just on/off diode with some color. To achieve this we should use a few commands:
pinMode
- Configures the specified pin to behave either as an input or an outputdigitalWrite
- Write a HIGH or a LOW value to a digital pinanalogWrite
- Writes an analog value (PWM wave) to a pin
To use analogWrite we can use any of the pins marked with the
~
symbol - 3, 5, 6, 9, 10, 11 for the Uno board
First version of program - simply on different colors in a loop:
255-red
used because of the connection way of RGB diode.
This is a simple fw that can show 3 colors in a loop. To make a bit complicated sample, we can add some point of control for it - the menu.
colorDiod.ino
RGBLed.h
RGBLed.cpp
Note: part of code generated by circuito.io
Now, if u upload this app to the board, u will send Command using CommandPallete and serial port and observe some changes on the diode.
Conclusion
I’m very excited about Arduino and its simplicity of it. Big community and open source projects make it possible to learn quickly and to get some additional knowledge.
Resources
- Download link
- Visual studio code
- Getting Started with Arduino UNO
- Language reference
- Use Visual Studio Code for Arduino
- Learn how you can use the Arduino extension on Visual Studio Code to create programs for your Arduino.
- Multiple source files support
- Enable input for the serial monitor for Visual Code
- About Programmer for Arduino
- Using an External Text Editor in Arduino IDE
- Debugging
- How to debug code in Arduino IDE
- Project hub
Share on: