For the second example of using analog inputs, I have to use a very common electronic component in almost any project: a potentiometer, or simply, a variable resistor, which is a component that by the use of some kind of knob, allows us to change the resistance it offers.
Potentiometers are all around us, from the volume knob on your sound equipment to the temperature regulator of your heating system.
For this project, I'll combine a potentiometer with the motor speed controller we built in Chapter 4, Controlling Outputs Softly with Analog Outputs, in such a way that we can easily change the motor speed by acting on the potentiometer.
Two typical potentiometers along with their corresponding schematic symbol are shown as follows:
The component itself has three legs. If you measure the resistance between the external two legs, you should get the total resistance of the potentiometer, but if you take the measure between the central leg, usually called cursor, and any other, you will get a resistance proportional to the rotational angle of the potentiometer.
In our project, we will use it as a kind of a throttle for our DC motor by making the speed of the motor a function, or dependent, of the potentiometer position.
Here, you have the project schematic. It's nothing new, as you can see. The input side of the circuitry is similar to the input part of the ambient light meter, and the output side is similar to that of the motor driver in Chapter 4, Controlling Outputs Softly with Analog Outputs.
I have once again used pin A0 to read the input and in this case, I will act on PWM pin 6 to control the motor speed though a transistor.
Next, you can find the breadboard connections diagram for the proposed circuit.
The only components you should care about are the diode and the transistor.
The diode should be correctly placed because of its polarity. The bar indicating its cathode should be in contact with 5V.
Regarding the transistor, I used a BC547 with its legs ordered as CBE in my assembly; keep this in mind or refer to your transistor datasheet to know its own pin out before connecting it.
The code for this project couldn't be simpler. Just read, convert, and output the mapped value to the motor. Here is the code:
/* Chapter 06 - Analog Inputs to Feel Between All and Nothing DC Motor speed control By Francis Perea for Packt Publishing */ // Global variables we will use // One for for the potentiometer // and another to command the transistor // Two variables to store read and converted values int pot = 0; int base = 6; int potvalue = 0; int motorspeed = 0; // Configuration of the board: three outputs and one input void setup() { pinMode(base, OUTPUT); pinMode(pot, INPUT); //optional } // Sketch execution loop void loop(){ // Read the sensor and convert to the // allowed output range for an analog output potvalue = analogRead(pot); motorspeed = map(potvalue, 512, 1023, 100, 255); analogWrite(base, motorspeed); }
Once again, I have used a little bit of calibration to get to the best values for the input and output ranges of the map()
function.
By using a 10K ohm potentiometer and a 10K ohm protection resistor, I got values from 512
to 1024
from the potentiometer.
Regarding the motor, outputting less than 100
was unable to make it spin, so finally, my conversion is motorspeed = map(potvalue, 512, 1023, 100, 255);
.
As I told you previously, if you can't wait to know how to calibrate your sensor readings, you can take a look at Chapter 8, Communicating with Others.