Introduction

The Internet of Things concept is fashionable, which refers to the ability to interact with physical devices, obtaining information/metrics (e.g., temperature, humidity, etc.) and sending commands/actions (e.g., opening the door, turning on air conditioning, etc.).
The concept, which is not new, implies collaboration between electronics professionals, programmers, and even DBAs. This book is suitable for developers and DBAs who have little knowledge of electronics, giving them an introduction to using Arduino, one of the most well-known electronics platforms used in this area.
In this section, we will make a brief introduction to the Arduino, which is basically a board with a programmable microcontroller, cheap, and easy to use.
The first thing we have to be clear about is that it is a microcontroller and how it differs from the microprocessor. We have all ever come into contact with both concepts, that is, we all have a computer at home, whether desktop, portable, etc.
Well, the core of our computers is a microprocessor, a chip in charge of performing complex operations from some instructions (which we will call program) and some input data obtaining some output data. To process and store this data, we need to connect the microprocessor to RAM and other I / O devices (Input / Output), which are connected through the motherboard.
Defined the microprocessor in a concise way, and taking into account that we had said that the microprocessor needs to be connected to the memory through the motherboard, in the microcontroller, we have both the memory where we store the program, and the memory where it is stored — data, in the same assembly (on the same chip).
Sorry if I have taken licenses to define it, I know there are several more differences, such as the location of the data memory and program in reference to the microprocessor.
Where Do I Find a Microcontroller?
We find it in most electronic devices that we use every day, such as remote controls, watches, televisions, cars, and much more. The importance of knowing how they work and how they are programmed opens up many doors.
Arduino
A few years ago, a free project, called Arduino , appeared,   which facilitated access to this class of devices for students since it is an open hardware-based board (its design is free and can be reproduced by anyone).
Initially, the board was connected through a USB port to program it (it usually is done based on its IDE that can be found here ). Arduino programming was not done at a low level with assembler like many microcontrollers (from now on we will call them PICs), but it is done with a language more understandable by most of us, C / C ++ (the basic reference to the language we found here and examples on this route ).
With these elements, a programmer who does not know about PICs would be able to program the Arduino in a short time.
The UNO version board consisted of 14 digital I / O pins, of which 2 had a serial connection. These pins are useful for most basic sensors, or for relays, actuators, etc., which only have two states, on or off (or with the Arduino constants HIGH and LOW). It also has six analog pins, capable of reading up to 1024 voltage levels, called analog port resolution. These are used to read sensors that give us different voltage ranges depending on their state, such as heat resistance, variable resistance, etc.
Basically, with the structure of the Arduino UNO, anyone can enter the world of PIC programming. But, for larger projects, more power is needed, with which the Arduino boys were creating plates and improved versions of the Arduino UNO. I expose a few:
Arduino Leonardo
It is the first evolution of the Arduino Mega, in fact, it has the same ports, but the PIC is different, with greater capacity and higher working frequency. Another difference is the USB port, which at the same time of being used to upload the program, can use the USB as a host; that is, we can use it as a keyboard, etc ... Another inclusion is the SDA and SCL ports, used for devices that communicate through the I2C protocol.
Arduino Mega 2560
For those who fall short, the Leonardo designed the Arduino Mega 2560, with much more speed in the microcontroller and many more digital (54 pins) and analog (16 pins) ports. We have the pins for I2C like Leonardo (although they change position).
This design is also the Arduino Mega ADK, which is a modified board of the Mega 2560, but a USB host port is added. Google has designed several projects with this board and its flagship, Android, an example you have on this page .
Another evolution is the Arduino DUE, with the same design as the Mega 2560 but with a 32-bit microprocessor. It is much faster than the Mega 2560.
Arduino YUN
Basically, this board has the same pin and processor characteristics as the Arduino Leonardo, but a microcomputer has been incorporated where a small Unix resides, which allows us to mount fundamental web servers. This board is mostly used for projects in which the sensors have to report through an Ethernet network quickly and easily.
More developments will come in the future, merging the power of Arduino boards with computer motherboards and offering us the possibility of creating a multitude of projects, with unlimited power and scalability.
Know the Hardware
There are several models of Arduino. Here we will just work with the Uno and Mega models.
The biggest differences between them are the speed, the amount of memory, and the number and type of pins. Some are connected via USB via a Type B USB connection, and others via a Type B Micro-USB connection. The positions of the Reset button and LEDs also vary by model and version.
For the purposes of this workshop, we will use pins and features that are common to everyone.
Arduino Uno
  1. USB for power and communication;
  2. Alternative power (from 9V to 12V, center pin is + );
  3. Power pins (outputs and inputs);
  4. Analog input pins (can be used as digital);
  5. Digital input / output pins ( ~ means PWM digital output)
  6. Processor;
  7. Reset button
  8. LEDs: RX and TX indicate serial communication (USB port), L shows pin 13 value.
Breadboards
Hello World with Integrated LED
After installing the Arduino IDE (https://Arduino.CC/en/Main/Software) and connecting it via the USB cable, you need to configure it. We start by choosing the serial port where it is connected (usually there will only be one).
Next, you must choose the Arduino model.
In some cases (such as Arduino Mega), it is also necessary to choose the version.
The Arduino IDE comes with some examples. Let's start by using the Blink example for flashing the integrated LED on board (L), which is connected to pin 13.
The example shows two functions:
Setup()
In this function, we initialize the hardware we are using. This function runs once when Arduino is powered.
Loop()
In this function, we put the program that performs the desired function. At the end of this function, it is called again (repeats indefinitely).
After creating the program, we must validate it. For this, we use the first toolbar button (√).
To send the program to Arduino, we use the second button (→). During upload, we will see several messages at the bottom of the IDE as well as the flashing RX and TX LEDs on the board. Once the upload is complete, the program starts automatically. In this case, the LED L on the board will flash.
You can adjust the delay()to change the duration of each state (on or off) of the LED. The delay()uses milliseconds. We start by implementing the following scheme, with a button and a high resistance (e.g., 10 kΩ).
This scheme, which, while the button is not pressed, is sending 0V to the input, is called a pull-down. There is also a pull-up, but by default, the input gets + 5V (or + 3.3V, depending on the type of card).
Then we load the Button example. After upload, the button will work to activate the LED. Using the scheme from the previous lab, we will send the status of the button over the USB / serial connection to the computer. Let's open the DigitalReadSerial example.
After sending the program to Arduino, let's see what is sent to the USB / serial port using Serial Monitor.
Note: Before turning on the Serial Monitor, in some, the serial output buffer fills up quickly, then the LED starts flashing randomly, and the program hangs on the Serial.println().
We can also send custom to debug text. For example, we add the line (always respect case).
[...] Serial. println ( buttonState );
if ( buttonState ) Serial . println ( "ON" ); else Serial . println ( "OFF" ); 
delay ( 1 ); // delay in between reads for stability [...]
Building on the scheme of previous labs, let's add a relay (which acts as a switch that can control external equipment).
We add to the end of the function setup():
pinMode ( 13 , OUTPUT );
pinMode ( 12 , OUTPUT );
We add to the end of the function loop():
digitalWrite ( 13 , buttonState );
digitalWrite ( 12 , ! buttonState );
Why is the pin 12 (relay) value reversed? The relays we are going to use will be active if their signal pin receives 0V, and they are inactive if they receive 5V (or not turned on). Not all relays are like this, so try testing before using it.
Using the scheme of previous labs, we will add an analog temperature or light sensor to pin A0.
Next, we load the example AnalogReadSerial.
We open the Serial Monitor again to see the sensor values. Analog pin reading (A0, A1, etc.) returns values between 0 and 1023 (i.e., linearly between 0V and 5V). However, likely, the sensor chosen will only show values in part of this range. Record the minimum and maximum sensor values for use in the following laboratory.
Using the scheme of previous laboratories, we will define rules for activating the relay. For example, if we use a light sensor, when we no longer have light, we want to enable the relay to turn on a lamp.
We add to the end of the function setup():
pinMode ( 13 , OUTPUT );
pinMode ( 12 , OUTPUT );
pinMode ( 2 , INPUT );
We add to the end of the function loop():
if ( sensorValue > 500 || digitalRead ( 2 )) { // Relay on
digitalWrite ( 13 , HIGH );
digitalWrite ( 12 , LOW ); } else { // Rele off
digitalWrite ( 13 , LOW );
digitalWrite ( 12 , HIGH ); }  
The exact value (here 500) and the operator ( >or <) may vary by sensor. Use the values obtained in the previous laboratory to calibrate.