﻿ ﻿Expanding Arduino - Giving Life to Objectss - The Maker's Manual: A Practical Guide to the New Industrial Revolution 1st Edition (2015)

# The Maker's Manual: A Practical Guide to the New Industrial Revolution 1st Edition (2015)

### Chapter 17. Expanding Arduino

Now that you understand the basics of Arduino, you can have fun and use it for many different purposes. Many ideas can be found online, some even come with full documentation on how to reproduce them. In Chapter 16, you saw that an Arduino is capable of interacting with sensors and actuators; let’s have a more in-depth look at these components.

A sensor is a component that allows you to measure a physical quantity or detect an event. The simple button you used in earlier exercises is also a simple sensor, which detects whether someone is pushing it. There are many different kinds of sensors. How do they work?

Thermistors

Thermistors (Figure 17-1) are based on the premise that some materials vary their electrical resistance as their temperature changes. The change is consistent, and can therefore be easily quantified: a resistance of X always means a temperature of Y. The ratio can be direct (the resistance increases as the temperature increases) or inverse (the resistance decreases as the temperature increases).

Figure 17-1. A thermistor.

Therefore, reading temperatures in Arduino is easy. Since a thermistor is an analog sensor, you’ll connect it to one of the ports between A0 and A5, then add a resistor and create a voltage divider like the one pictured in Figure 17-2. In this way, part of the supply voltage (5V) will be applied to the thermistor, and part of it to the resistor. If you read the incoming voltage value on the analog pin (i.e. the voltage at the ends of the thermistor) and you know the relation between resistance and temperature, you can determine the temperature.

Figure 17-2. A thermistor in action

In the sensor documentation you can usually find all the information you need to convert the readings of Arduino inputs into the quantity you want.

Photoresistors

Other materials react to light. Their resistance changes when they are illuminated. These materials are used to make photoresistors (Figure 17-3): small disks that change their resistance as the light level changes.

Figure 17-3. A photoresistor

The circuit needed for a photoresistor (Figure 17-4) is identical to the one you have just seen for the thermistor. Many other kinds of analog components are connected in the same way. You typically want to use a resistor that matches the range of the photoresistor in your voltage divider. For a photoresistor with a maximum resistance of 10K, use a 10K resistor.

Figure 17-4. A photoresistor is connected like a thermistor

With both a photoresistor and thermistor, you can use analogRead, as described in “Beyond Digital”.

Other Kinds of Sensors

There are other materials which change their resistance if put under pressure, are bent, or get exposed to humidity or water: the same principle is valid for all of them, and each one can be used to make a sensor. There are also more complex sensors (Figure 17-5) which are able to measure the presence of specific gases and substances, detect the earth magnetic field, the acceleration sustained by an object, or other phenomena. Basically, there is probably a sensor for anything you want to measure. Even radios and cameras can serve this purpose.  For instance, with a webcam and a series of software libraries for image editing you can identify colors, shapes, and even people (though you’d need a Raspberry Pi or personal computer to have the computing power needed to process such things).

Figure 17-5. Some kinds of sensors

Other sensors can be queried through a serial port, and there are libraries that allow you to use them with Arduino. There are sensors of all sizes and different prices, hence the only limit is your imagination. Some exercises you could try with sensors:

§ Make a circuit that switches a series of LEDs on when the ambient light falls below a certain threshold (for example, when you struggle trying to read a book);

§ Modify that circuit so that the LEDs fade up gently as the ambient light fades out;

§ Using your favorite technology (such as a laser cutter), make a table lamp that uses that circuit.

Changing the World: Actuators

Some components carry out a complementary function to sensors, in that they act on their surrounding environment, instead of merely reading it. These components are called actuators, and they usually turn electricity into movement, light, heat, or sound. You have seen your first example of actuator when you made your Hello World of electronics, that is, when you switched an LED on, which is one of the simplest actuators: when crossed by current it emits photons, turning electricity into light.

Buzzers

Another kind of actuator is the piezoelectric transducer, more familiarly called a buzzer. The diaphragm of the device expands and contracts as the voltage changes, thus producing a vibration. Its frequency depends on the variation of the applied voltage: basically, it turns electricity into vibrational mechanical energy. You can try and use one to play a short tune with Arduino if you connect the components as in Example 17-1.

To make your buzzer buzz, you will use the tone function, which accepts pin, frequency, and duration as parameters. The last parameter is optional, but if not specified the buzzer will keep on buzzing until you use the noTone function. Example 17-1 shows an example sketch.

Example 17-1. You can make Arduino play a tune

void setup()

{

pinMode(9, OUTPUT);

}

void loop()

{

tone(9, 131, 100);

delay(500);

tone(9, 147, 100);

delay(500);

tone(9, 165, 100);

delay(500);

tone(9, 175, 100);

delay(500);

tone(9, 196, 100);

delay(500);

}

Figure 17-6. Connecting a buzzer to Arduino with a 100 ohm resistor

WARNING

On boards other than the Arduino Mega, the tone function interferes with the PWM on pins 3 and 11.

Servos

Another example of an actuator which turns electricity into mechanical energy is the servo, a particular kind of motor capable of moving an arm connected to it into a specific position and holding it there. Usually, it can perform rotations of up to 90 or 180 degrees. You could use it for many purposes, such as turning a doll into an animatronic machine with independent movement capability. Let’s try to make a circuit with a servo and a potentiometer which you will use to control it (Figure 17-7).

Figure 17-7. A servo controlled by a potentiometer

Thanks to the Servo library, which manages the PWM signal that drives the movable arm, using a servo is very simple (Example 17-2).

Example 17-2. Controlling a servo with a potentiometer

#include <Servo.h>

Servo myservo;

int position = 0;

void setup()

{

myservo.attach(9);

}

void loop()

{

position = map(pos, 0, 1023, 0, 180);

myservo.write(position);

delay(100);

}

After including the library with the #include<Servo.h>; statement, the sketch creates a variable myservo which represents your servo. In setup, the sketch attaches myservo to pin 9.

In loop, the sketch takes the value read by the sensor connected to the potentiometer and converts it into degrees, so that the maximum value, 1023, corresponds to 180°. The sketch then tells the servo to rotate the movable arm to the determined position, and wait 100 milliseconds to allow for the rotation. Now you can modify all the dolls you have at home! You can even cut out a board with the names of your colleagues and use the random function to generate a random number and move the servo arm, so that it indicates who’s due to bring in donuts the next day:

int num = 10; // # of colleagues

int chosen = random(num);

position = map(chosen, 0, num, 0, 180);

myservo.write(position);

Before you move on, try some exercises:

§ Complete the donuts sketch and change it so that you’ll have to bring them in less often (after all, you are the one who creates the hack, right?);

§ Change that sketch so that the arm stops at the center of each section, in order to avoid arguments when it is not clear who’s going to bring in the donuts the next day.

Strong Currents

The Arduino can manage current up to 40 milliamps per pin: this value is very small if you consider that a simple MP3 player requires something like 100 milliamps, and an electric oven around 30 amps. If you wanted to use an Arduino to command the oven to start cooking dinner, there’s no way the Arduino could supply the power the oven needs. And any attempt to push 30 amps through an Arduino would end up with a roasted circuit board, not a cooked dinner.

This does not mean that you can’t connect your oven, or a lamp, or even a 12 volt motor to an Arduino, only that you have to go about it a different way. You simply have to use specialized electronic components to separate the Arduino’s weak current from the much stronger current in the rest of the circuit.

For this purpose you can use a device called MOSFET (acronym for Metal-Oxide-Semiconductor Field-Effect Transistor) whose functioning is very simple. Think again of our comparison with water pipes (Figure 17-8): imagine an irrigation valve that is activated when the timer linked to it sends the pertinent signal. The valve has three ports: a Source flowing in, a Drain flowing out, and a Gate that controls whether the valve is open or closed.

Figure 17-8. The hydraulic analogy of a MOSFET.

MOSFET works in an analogous way (Figure 17-9): when a (weak) signal gets to the Gate, the MOSFET allows the (strong) current to flow from the Source to the Drain

Figure 17-9. Functioning scheme of a MOSFET with a protection diode for inverted currents (indicated in red)

With a MOSFET you can easily control actuators that need more current than can be managed by an Arduino alone--for example, an electric motor or a stepper motor--with the additional advantage that you can use voltages different from the ones supplied by the board. With a properly rated MOSFET (one that could handle the voltages and amps required) it should be relatively easy to control a large device from a small Arduino. Figure 17-10 shows the circuit for this. Note the use of the diode to protect Arduino from any current that the motor produces itself (since a motor can also be a generator when you spin it by hand).

Figure 17-10. Arduino controls a direct current motor powered at 3V.

Shields

Arduino is not only very versatile, but expandable too. To use particular sensors or actuators, it is often necessary to create quite complex circuits comprising microprocessors and specific components. To meet the beginners’ needs and to let experts spare some time, many producers have created a series of boards called shields, only a few of which are shown in Figure 17-11, that recreate these circuits and plug directly into the Arduino. Most of these shields are designed to be modular, so you can integrate Arduino with even more shields and create a stack which resembles a huge sandwich and has much more functionality. For example, you can create a green station to monitor environmental parameters (using one dedicated shield with specific sensors), store the data (using another shield with external clock and datalogger) and publish them (using a Wi-Fi or Ethernet shield).

Figure 17-11. Shields for Arduino.

Each shield has its own specific functions and its own documentation; some also require the use of additional software libraries to access these new functionalities from your development environment. You can buy shields in most places where Arduinos are sold.

Smart Textiles

Technology allows you to use new tools and materials to give life to your creations. Those that you can wear and which allow you to combine electronics with fashion are known as wearables. In fact, some types of materials, in particular conductive textiles (Figure 17-12) and conductive threads, enable you to create real electrical circuits; these fabrics are generally known as smart textiles, even if the most correct term would be e-textiles.

A further advantage offered by these textiles is that they can be washed without being damaged, although, before starting the washing machine, you must not forget to remove the components, which may otherwise get damaged.

When developing wearables, our suggestion is to carry out the project iteratively, testing that everything is being processed in the proper way as you proceed towards the final result.

Figure 17-12. A conductive textile can replace the electrical threads in a circuit.

One of the problems you have to face with wearables concerns the power source--most of us would find being constantly attached to a power adapter rather confining, and constantly having to replace batteries annoying and wasteful. Another limitation is that, although Arduino is a compact board, if you have to attach it onto a jacket or pair of pants it may feel bulky and awkward. Here the solution is LilyPad Arduino (Figure 17-13), a simplified version of Arduino specifically designed to be used with wearables. The LilyPad has some eyelets covered with a conductive material that can be used to stitch your board to fabrics with some conductive threads, creating a real circuit. You can also use a small rechargeable battery with the LilyPad.

Figure 17-13. A LilyPad circuit attached to a fabric with conductive thread.

One big difference between the LilyPad and Arduino Uno is that the LilyPad has fewer pins than the standard Arduino, and these pins are well separated from one another in order to avoid any accidental short circuit. Another difference is that there is no classical USB connector, so it is necessary to use a specific programmer, i.e. a small board that converts signals from USB to serial communication. In addition, to solve the problem of power consumption, the LilyPad is equipped with a button that can switch off the circuit when not in use; however, when you connect the LilyPad to a computer, the LilyPad computer will always be powered regardless of the position of the switch.

There are also LEDs designed for wearables (Figure 17-14). They are smaller and are assembled on small boards with two holes for stitching them, just like LilyPads. Of course nothing prevents you from using traditional LEDs, and the possibility of choosing between different solutions you prefer for a particular project offers you more freedom of use.

Figure 17-14. Sewable LEDs

Before using a LilyPad, it is advisable to test the circuit with a classical Arduino because it is much easier to add, move or remove links. Once you have tested everything, you can load the sketch on your LilyPad, connect it to the rest of the circuit, and test your fantastic artifact!

﻿