Arduino microcontroller programming for beginners
By: J-Labs Software Specialists
Tags: DIY, Arduino

Check how simple and easy it is to program Arduino microcontroller.

It's compact, powerful, goes with hundreds of additional components that allow you to feel how it is to be a real engineer.

Let this guide inspire you to build your own devices, micro machines, smart-house components and many more!

Set up your board and IDE

Step 1 - you need to have an Arduino board. There are many types of boards as well as many clones, for a start, Arduino Uno is the cheapest choice.

Step 2 - Download Arduino Ide software. Download IDE software compatible with your OS from the official page https://www.arduino.cc/en/Main/Software .

Software is working with the original board and also with cheaper clones.

Step 3 - Power up your board. Connect Arduino board to PC with a USB cable.

Step 4 - Launch and configure IDE.

File ->New for starting a new project

Tools -> Board for a select type of board you are using

Tools -> Serial port for selecting a port board is connected to

Now you are ready to write code.

Code structure

The Arduino software is open-source. The source code for the Java environment is released under the GPL and the C/C++ microcontroller libraries are under the LGPL.

Code Structure consists of two main functions:

· Setup() function

· Loop() function

Void setup () {

}

The setup function is used to initialize variables, pin modes, attach libraries. This function will only run once after powering up a board or after reset.

Void loop () {

}

This function loops consecutively allowing the program to change and respond. Actively control the board

Contain the main program body.

Building a project

In this tutorial, I will use two Arduino modules to build a simple dust/pollution detector.

I will use:

DMS501 dust sensor

the simple two-line LCD display (with rotary potentiometer) 2 lines with 16 letters to present a measuring result.

As a controller, I’m using Arduino Mega (clone) but all used modules can be attached to an Arduino board.

All of the modules that can be connected to microcontrollers have documentation with connection details.

Note:

There is large variety of modules that can be attached to Arduino, and its connecting pins may not be the same.

Producers always provide necessary support information. Search over the Internet how to connect your modules,

There is plenty of tutorials for whatever display/module type you got.

There may be even a few ways to connect each module, after building a few contraptions you will find you how to do it.

Connection pin diagram for LCD, as stated before, its not only way to do this with 2x16 LCD:

Pin Description:

1 Ground (0V) — Ground

2 Supply voltage; 5V (4.7V — 5.3V) — Vcc

3 Contrast adjustment; through a variable resistor — VEE

4 Selects command register when low, and data register when high — Register Select

5 Low to write to the register; High to read from the register — read/write

6 Sends data to data pins when a high to low pulse is given — Enable

7 8-bit data pins — D80

8 8-bit data pins — D81

9 8-bit data pins — D82

10 8-bit data pins — D83

11 8-bit data pins — D84

12 8-bit data pins — D85

13 8-bit data pins — D86

14 8-bit data pins — D87

15 Backlight VCC (5V) — Led+

16 Backlight Ground (0V) — Led

And dust sensor:

Complete connection schema :

Code — variables and libraries

This C++ code is universal and will work on any Arduino board or clone.

I have separated all variables to the first section of code.

In the first part, I’m including libraries for both modules to use it predefined methods later.

Another part is to define PINS to which cables are actually connected.

Setup ()

The setup part contains code that will be executed on start — once only.

Initialization for two modules.

Loop for warming up a sensor.

Printing info to console and to LCD

Loop ()

Actual code that will be executed on the device in an endless loop.

Requesting data from the sensor and clearing LCD to remove counting text.

I’m using dsm501 library methods to fetch the density of particle information

to be later printed on LCD. I’m using this library because basically this sensor provides particles per square feet and these units don’t tell much in our country.

Lib contains not a trivial calculation to convert it to µg/m3 (microgram per square meter)

Console

Entering Tools->Serial Monitor will show console

Final device

Summary

Arduino is a user-friendly microcontroller, easy to cheap and easy to start.

There are many different boards on the market and hundreds of different sensors, motors, modules etc.

That allows building useful and complicated devices. All of it is well documented and there are lots of guides, YouTube videos, and forums that will help to start, solve problems and make your life easier.

A world of small electronics stays open for anybody who wishes to try it.