• No se han encontrado resultados

Using a multiplexer, it is possible to make the Arduino read over a hundred buttons easily. A multiplexer/demultiplexer is an integrated circuit that selects one of several inputs and forwards them to the output. It requires a few control pins to determine which input to forward to the output.

Getting ready

Following are the ingredients required for this recipe: f An Arduino board connected to a computer via USB f A breadboard and jumper wires

f Four buttons

f A 4051 multiplexer or similar, which we can find at any electronics store and online at Digikey, Sparkfun, Adafruit, and so on

How to do it…

We implement a simple configuration using only four buttons. Here are the steps:

1. Connect the Arduino GND to a long strip on the breadboard. Also connect the Arduino 5V to a long strip.

2. Mount the four buttons and connect one of their terminals to the long GND strip. 3. Connect the other terminal of each button to an individual input/output pin on the

4051—in this case, pins y0, y1, y2, and y3.

4. Connect the E, VEE, and GND pins of the 4051 multiplexer to the long GND strip. 5. Connect the Vcc pin on the 4051 to the 5V strip on the breadboard.

6. Connect S0, S1, and S2 to three digital pins on the Arduino—in this example, 8, 9, and 10.

Schematic

This is one possible implementation. Other pins can also be used.

Code

The following code will read the four buttons connected to the multiplexer by switching the active pin on it:

// Define the input pin on the Arduino and the 3 selection pins

connected to the 4051 int buttonPin = 2; int A = 10; int B = 9; int C = 8; void setup() {

// Define pin #2 as input with the pull up resistor on

pinMode(buttonPin, INPUT_PULLUP);

// Define the output pins going to the control lines of the Multiplexer

pinMode(A, OUTPUT); pinMode(B, OUTPUT); pinMode(C, OUTPUT);

// Establish the Serial connection with a baud rate of 9600

Serial.begin(9600); }

void loop(){

// We first read port IO0

digitalWrite(A, LOW); digitalWrite(B, LOW); digitalWrite(C, LOW);

int buttonIO0 = digitalRead(buttonPin);

// Then we read port IO1

digitalWrite(A, HIGH); digitalWrite(B, LOW); digitalWrite(C, LOW);

int buttonIO1 = digitalRead(buttonPin);

// Then we read port IO2

digitalWrite(A, LOW); digitalWrite(B, HIGH); digitalWrite(C, LOW);

int buttonIO2 = digitalRead(buttonPin);

digitalWrite(A, HIGH); digitalWrite(B, HIGH); digitalWrite(C, LOW);

int buttonIO3 = digitalRead(buttonPin);

// Then we print to Serial the values

// We print them in-line separated by a space

Serial.print(buttonIO0); Serial.print(" "); Serial.print(buttonIO1); Serial.print(" "); Serial.print(buttonIO2); Serial.print(" "); Serial.println(buttonIO3);

// Delays the execution to allow time for the serial

delay(25); }

How it works…

The multiplexer/demultiplexer is a useful component, but, a little tricky to understand.

Here we used a demultiplexer configuration. Each demultiplexer has one output and a number

of inputs—in our case, eight. Also, it has control lines—in our example, three. Each control line represents a number: power of 2 minus 1. For the 4051, A = 1, B = 2 and C = 4. If we want to read input IO5, we set A and C to HIGH and S1 to LOW. This means the output will be connected to A + C = 5 input; therefore, pin IO5.

Basically, a multiplexer gives the power to connect one Arduino pin to one I/O pin on the multiplexer. Only one pin can be connected at any particular time.

Code breakdown

The code commands the connection on the multiplexer using the three command lines. It uses one input digital pin to get the value from the buttons and prints it on the serial connection. Here, we declare the used pins:

int buttonPin = 2; int A = 10;

int B = 9; int C = 8;

In the loop() function, we set the multiplexer to each pin we want to read and we read it. In order to read pin IO0, we set A, B, and C to low, so their sum is 0. When we want to read pin 1, we set A to 1. IO3 will result A and B to HIGH:

digitalWrite(A, HIGH); digitalWrite(B, LOW); digitalWrite(C, LOW);

int buttonIO1 = digitalRead(buttonPin);

We do this for each button we want to read and then we print the output values on the serial.

There's more…

Here we have only four buttons on four pins—not a very good ratio of pins to buttons. However, for the same number of pins we can get eight buttons, as there are four free pins on the multiplexer.

More buttons

Even eight buttons on four pins is not too much. There are 16-channel multiplexers, such as

the 4067 that require four control lines, totaling sixteen buttons on five pins. We can go even

further! We can use more multiplexers, and we only need one new line for each multiplexer to connect to its output while sharing the control lines. Using a 4067 and all the pins, except 0 and 1, on the Arduino Uno, we can read 224 buttons. On the Arduino Mega, this will result in 800 buttons. The sky is the limit with multiplexers.

See also

For an in-depth explanation on multiplexers, visit http://en.wikipedia.org/wiki/ Multiplexer.

4

Sensors

In this chapter, we will cover the following topics:

f Simple sensor – potentiometer f Temperature sensor

f Detecting motion – PIR Sensor

f Measuring distance – infrared and ultrasonic f Noise reduction

f Accelerometer f Localization – GPS

Introduction

Acquiring data from the environment is the fundamental function of any autonomous system.

And on the Arduino, this feature is so simple and powerful. We can find sensors for anything

these days, from high radiation to sound. Most of them even share the same interface, so connecting and using them is easy once we understand the simple logic underneath. In this chapter, we will dive into the most common groups of sensors and we will see how easy it is to use them.

We can acquire interesting and useful sensors from DIY electronics and robotics shops.