Arduino is all the rage for controlling the low-level pieces of a DIY home automation system.
That’s primarily because of the easy-to-use Arduino code that makes controlling the input and output of a microcontroller (a small and cheap computer) much simpler than it has ever been before.
In this article, you will learn two Arduino functions for reading inputs, and one Arduino function for creating outputs. The Arduino functions you will learn below are so fundamental to using the powerful features of a microcontroller, that I recommend you memorize their use.
Arduino and I/O
The first set of Arduino functions we will lock into your brain are functions for reading inputs. But before we dive into those, I want to
make sure we have a groundwork laid out for understanding Arduino.Arduino is really three things:
- Hardware (like an Arduino UNO R3, or an Arduino Nano)
- Software that you use to program the hardware (the Arduino IDE),
- It is the wider eco-system of code libraries available for easily using things like bluetooth and wifi modules, and many different kinds of sensors and motors.
What I want to focus on from above is number 1 – the hardware. Here’s the deal, every Arduino board utilizes a microcontroller. And like I mentioned before, a microcontroller is a super cheap and very small computer.
Microcontrollers are superbly designed to read inputs, and create outputs. Now you have probably heard the term I/O (pronounced: eye-oh) before. It stands for Input/Output.
Arduino’s universe is centered around I/O. In terms of a microcontroller, inputs and outputs are changes in voltage levels. Inputs are when a microcontroller can “see” a voltage change, and outputs are when a microcontroller creates a voltage change.
So let’s talk about Arduino functions that can help us read voltage changes…
Reading Inputs with Arduino
Most home automation programs are going to involve reading inputs into a system. The inputs will likely come from sensors you have wired to your Arduino board.
Generally, the inputs will come in two separate varieties – either binary (on/off, digital inputs), or a range of values (analog inputs).
Said in another way, some sensors produce an On/Off signal, and other sensors produce a range of signals. Learning the difference is pretty intuitive; just ask the question “what does this sensor tell me?”
If the answer can only be one of two options, it is a digital input. If the answer can be a whole range of values, it is an analog input.
Let’s do some quick practice…
- You have a reed switch that detects if a door is open or closed. Will this produce a digital or analog input?
-
- That’s right, a digital input, because there are only two options.
- You have a temperature sensor that works in the −50° to 300°F range. Will this produce a digital or analog input?
- That’s right, an analog input, because there is an entire range of possible values.
The Arduino has 2 separate functions for reading these different types of inputs.
Arduino analogRead()
For reading analog inputs, there is the function called analogRead() (good name, eh?). It looks like this in actual Arduino code:
1 |
analogRead( pin# ) |
If you want to use the analogRead() function you’ll need to connect your sensor circuit to one of the analog pins on your Arduino.
All the Arduino boards have analog input pins. For example the Arduino UNO has 6 analog pins, the Arduino Nano has 8 analog pins, and the Arduino Mega has 16 analog pins.
The analog pins on an Arduino board utilize the Analog-to-Digital-Converter (ADC) that is built into the board’s microcontroller. The ADC takes the analog input signal, and converts it into a digital signal that allows you to easily read the different values of the input.
In order for the analogRead() function to work, it needs to know which pin you want to use. Again, this will be one of the analog pins where you have your sensor circuit attached.
It might look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const byte tempSensor = A4; int rawTemp = 0; void setup() { } void loop() { /* Read analog input */ rawTemp = analogRead(tempSensor); /* Convert to actual temp... */ /* Do something if... */ } |
In the code above we have our temperature sensor attached to pin A4 of the Arduino. When we use the analogRead() function, we save the value to the variable rawTemp. Why rawTemp, instead of just temp?
Recall that an input is actually a voltage signal. Most often the voltage signal that is read by the Arduino ADC will need to be converted from the raw signal into the actual value. This is done using a conversion factor that is readily available from the sensor manufacturer, or by just doing a simple google search.
But what about those On/Off inputs, how do we read those with Arduino…?
Arduino digitalRead()
For digital inputs, Arduino has a function called digitalRead() (they really know how to name things!)
In Arduino code, this is what it looks like:
1 |
digitalRead( pin# ) |
To use the digitalRead() function, you need to have your sensor attached to one of the digital pins on your Arduino board (Pro Tip: Most analog pins can also be used as digital pins – check the documentation on your Arduino board to make sure).
All the digitalRead() function needs to know is where (which pin) your circuit is attached.
It might look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
const byte reedSwitch = 5; boolean doorState = 0; void setup() { } void loop() { /* Read digital input */ doorState = digitalRead(reedSwitch); /* Do something if... */ } |
In the code above, the Arduino board is reading the “state” of the door and storing it in the doorState variable.
Depending on the type of reed switch used, and based on the circuit you have set up, the digitalRead() function will return either a 0 or a 1 if the door is open or closed.
Knowing if a door is open or closed, or determining the temperature of something is pretty useful, but only if you can actually do something with that data.
To make things happen using our Arduino, we need to start creating outputs…
Creating Outputs with Arduino
Just like an input into a microcontroller is really just a voltage, an output from a microcontroller is also a voltage. The voltage an Arduino outputs from each pin depends on what type of Arduino board you are using. It is likely 5 volts, or 3.3 volts.
The voltage you output can be used to control something you have attached to your Arduino. It is important to understand that a microcontroller is designed to control circuits, but not necessarily power them.
This is an important distinction because an Arduino can only supply so much current through each pin before it gets too hot and breaks.
So while you may be tempted to push the limits of your Arduino output pins, it would be wiser to find a simple control circuit using one of the many pre-made modules available to do so (or just wire your own)
So how do we actually create an output with Arduino?
Arduino digitalWrite()
To create an output with Arduino, we use the handy digitalWrite() function.
In Arduino code, it looks like this:
1 |
digitalWrite( pin#, value); |
The digitalWrite() function needs to know two things.
-
- What pin to output voltage at
- What voltage to output, HIGH or LOW
In order for the digitalWrite() function to work, you need to use a digital pin on your Arduino board (just like before you can use the analog pins as digital pins – just make sure to double-check your board’s documentation).
You also need to tell the Arduino that the pin is going to be used as an output. To do this, you use another function called pinMode().
It might look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
const byte relay = 8; void setup() { /* Set relay pin as OUTPUT*/ pinMode(relay, OUTPUT); } void loop() { digitalWrite(relay, HIGH); //relay ON } |
In the code above, the pinMode() function is telling the Arduino board that we want the relay pin to act as an OUTPUT. The digitalWrite() function is then telling the pin to output a HIGH voltage. Which will be either 5 volts or 3.3 volts depending on your Arduino board.
The voltage at the pin will stay HIGH until we tell it otherwise. To change it to a low voltage, we would use this:
1 |
digitalWrite(relay, LOW) |
The code above would set the voltage at the relay pin to 0 volts.
- Now you may be wondering why we didn’t have to tell the pins to be inputs – and that is a good question! We can use the pinMode() function to tell a pin to be an input, but the Arduino defaults all the pins to act as inputs, so you don’t have to do it explicitly.
Now the real fun happens when we combine our newly learned functions and create some logic…
Read the code below, and see how we have combined reading an input and creating an output based on the state of the door:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
const byte relay = 8; const byte reedSwitch = 5; boolean doorState = 0; void setup() { /* Set relay pin as OUPUT*/ pinMode(relay, OUTPUT); } void loop() { /* Read digital input */ doorState = digitalRead(reedSwitch); /* Turn on relay when door closes */ if (doorState == HIGH) { digitalWrite(relay, HIGH); //relay ON } else { digitalWrite(relay, LOW); //relay OFF } } |
Know your core Arduino I/O functions
With the plethora of sensor modules, and matching useful code libraries, there is a nearly endless variety of systems you can use to automate your home using Arduino.
And while you learn about all kinds of other Arduino functions to make things like wifi, bluetooth, and motor control work, you’ll always come back to using these 3 core Arduino functions.
For repetition’s sake, let’s review them here:
- analogRead() – read an analog input (a range of possible values)
- digitalRead() – read a digital input (either On or Off)
- digitalWrite() – create a HIGH or LOW voltage output at a pin
As you can imagine, the possibilities are endless – which really brings us back full circle to why Arduino is such a common option when building DIY home automation projects.
About the Author:
Michael Cheich is the founder and lead instructor at Programming Electronics Academy, an online training website that teaches people how to program Arduino. He is a programming and electronics hobbyist who enjoys learning and tinkering with code and circuits. You can learn more about the Arduino training program offered by Programming Electronics Academy at www.programmingelectronics.com