Arduino - Sensors and Actuators

Introduction: Arduino - Sensors and Actuators

About: I work with computer networks services and security. My main hobbies: homebrewing, woodworking, electronics.
More About IoquirS »

Arduino is popular microcontroler. It’s cheap. There’s lots of sensors that can be uses with Arduino, as well as actuators.

But exactly what Arduino can be used for?

Well, this is not simple to answer. Or maybe it is: Arduino can be used for controlling whatever you want. Ok, ok! It’s not that much, let me restate: if you respect Arduino memory and cpu limitations, it can be used in small projects to control a lot of things. As “control” I mean monitor some situation, like temperature, umidity, light, and react when a threshold is reached, for example turn on a pump, light, heater, etc.

What you’ll use Arduino for depends on your needs. You may use it to learn robotics, feed your pet, control a simple robot, monitor your house temperatures, or even to help you making kraft beer (linK).

Depending on your needs, you’ll ned to use sensors and actuators, you’ll need to use or build (write) some code. As I wrote before, there’re lots of sensors and actuator that can be used with Arduino. Which ones will you use is very specific to your project.

This instructable goal is to present some basic concepts, sensors and actuators. We have a input button, distance and temperature sensors, a potentiometer as an analog input, some leds, a servo, a relay and a lcd display as output or “actuators”.

Code will be explained in parts.

Step 1: Parts Needed

Disclaimer: I use sparkfun links because they're shorter than the other sites I usually buy my arduino stuf.

Please feel free to reuse any component you have and buy from the nearest or easiest store for you.

;-)

- Arduino Mega (you can use a smaller one, just change pins assignments)

Step 2: Some Concepts

What is a sensor?

In simple words, a sensor is a device that “listens” to the physical environment and tells you what happens . Each sensor is able to listen to a specific input, that can be light, heat, motion, moisture, pressure, or any one of a great vairety of other environmental phenomena.

What is a actuator?

In the strict meaning, actuator is a device that converts energy in movement. It could be a valvle or a motor. But I preffer not to be so strict, I consider an actuactor anything that can convert electric energy in an output, for example a display, a led, a loudspeaker.

Digital and Analog

Information may be represented and trasnmitted both in analogic or digital format. The difference between them is that in analogic technology information is represented by amplitude variations, while in digital it’s represented in binary format.

Arduino can do analog and digital inputs, and only digital outputs. The sensors themselves usually are analog, but most of time they have a small circuit that translates values into digital format. In some cases you’ll find both connectors on the same device.

So, analog pins on Arduino are always input. Digital pins can be input or output, your code will tell wich.

Libraries

In programming, library is a piece of code that someone else previously wrote that can help you avoinding reinventing the weel.

Most, if not all, of the peripherals that can be attached to an Arduino have a library. The library controls the hardware, hiding complexity behind some simple routines that can be used by us.

Step 3: Start Building Your Toy

Let’s assume thatyou’ve already been presented to the Arduino IDE and know some about the C language used by arduino.

I have some code organization rules: my code starts with some remarks about the project, I always include libraries at top of the program and the declare all global variables and constants next. I like to use procedures and functions, to make code easier to read and maintain.

Besides these, remember that Arduino code has two mandatory structures: the setup and the loop procedures. The first runs once when the controller is started and the other runs until it’s turned off.

General

Use a small proto board to help making connections.

At first, connect Arduino power pins to the board:

5V to + line

GND to - line

Step 4: ​LCD Display

The LCD in this project was used in Nokia cell phones. It’s cheap, may show graphis as well as text in different sizes. If you use other than Adafruit_PCD8544 lybrary, you can chance text and numbers style.

Test your code now and see if display shows the distance, put your hand in front of sensor to see if it changes. If not, review wire connections and code.

Step 6: ​Distance Luminous Alarm

Imagine you want something to happen when distance measured by your sensor is too short. It may be a robot avoiding a obstacle, turning on a ceiling light, ringing a bell.

In our project we’ll turn a light on. Arduino ports can drive only a little power, so at this point we’ll turn on a LED. In order to control big loads you’ll need an external driver or power circuit, later on I’ll give an example, see Temperature alarm bellow.

LEDs must (almost) always use resistors to control current, or they won’t last long. For my hight bright white led I used a 330 ohm resistor. You may try different LEDs that will require different values for the resistor. See this instructable: https://www.instructables.com/id/Choosing-The-Res...

If it not works, review your wires, maybe your sensor has different colors for each pin.

Step 8: ​Temperature Alarm

Beyond just showing a temperature, you’ll probably want to change it somehow. Maybe turn on a heater or an cooling system. In this example, I’ll use the simplest cooler possible – a table fan. This fan works on A/C 110V and uses an amout of watts much greater than an Arduino DC 5V port can delivery. To make it possible to Arduino control the fan, we’ll use a one channel relay module with optocoupler isolation.

Connect the relay module:

VCC to proto board + line

GND to proto board - line

IN to Arduino pin 13

Connect the load:

Electical appliances have two main wires: live wire and neutral wire. In some cases there’s a third one, the ground wire. It’s necessary to interrupt the live wire and make it go through the relay. Then, conect:

A/C source ground wire to fan ground pin

A/C source neutral wire to fan neutral pin

A/C source live wire to relay module COM connector

relay module NO connector to fan live pin

No library is necessary now, just define the pin used with the relay module:

Test your code. If temperature is greater than 27º Celsius, relay will turn on and a indicator led on the module will light. If you connected an appliance like a fun, it should turn on.

Step 9: ​Push Button and Traffic Lights

A push button is probably the simplest Arduino input method, so we have to somehow use one. It will allow the user to manually control when one traffic light closes and the other one opens, like in a street crossroads.

Your traffic lights shoud be one “red” and the other “green” and change every time you push the button, passing by the yellow light.

Step 10: ​Potentiometer As Analog Input and Servo Control

A potentiometer is a variable resistor with a terminal that can be adjusted to any value from zero to it’s max resistance. Potentiometers are widelly used, for example in the volume control of a computer’s speaker. In Arduino programming, a potentiometer may be used as a analog input, but to represent digital values. The function “map” allows you to re-map a number from one range to another, for example let’s say the potentiometer min returned value is 5 and the max is 900, but we need to translate this values to the range 0~180, the map function will be:

result = map (ValueRead), 5, 890, 0, 180);

In a simplified view, a standard servo is a motor that is able to position the shaft at various angles, usually between 0 and 180. Arduino libraries allow the programmer to tell the servo to wich angle it must move and stay there until the next command.

Connect the potentiometer:

Connect the right terminal to proto board + line.

Connect the left terminal to proto board - line.

Connect the midle terminal to Arduino pin 1.

Connect the servo:

Connect the black wire from the servo to the GND pin on the Arduino.

Connect the red wire from the servo to the +5V pin on the Arduino.

Connect the yellow or white wire from the servo to a digital pin on the Arduino.

Connect the red wire to proto board + line.

Connect the black wire to proto board - line.

Connect wire left to Arduino pin 51.

Define the servo’s library:

#include <Servo.h>

Define the potentiometer pin:

// analog PINS definition:
#define Potentiometer 1

Define the Servo pin:

// digital PINS definition:#define
ServoPin 51

Declare the servo object:

Servo myservo; // create servo object to control a servo

Inside the “setup()” procedure type:

myservo.attach(ServoPin); // attaches the servo on pin defined by ServoPin