IAN LANG ELECTRONICS

Following on from the last chapter, we'll now look at the joystick as a means of controlling a motor. Firstly we'll look at a solution that, whilst it works, is unsatisfactory in a number of ways. Here's a wiring diagram:

A Motor Direction/Throttle Control

Go Back

The code to work this is as follows:

 

void setup(){

  pinMode (3,OUTPUT);}

 

  void loop(){

    int motorspeed;

  motorspeed=analogRead(A1);

  motorspeed=motorspeed/4;

  analogWrite(3,motorspeed);

  }

 

At startup the motor will begin to spin. If you push the joystick left along the horizontal axis, the motor slows down, and right, it speeds up. This is rather unsatisfactory for the following reasons:

 

1. The motor begins to spin immediately.

2. There is no control over the direction of rotation.

3. The degree of control of the joystick is not balanced; one way has more effect than the other.

 

We could use a more satisfactory solution simply by adding a relay in the circuit and changing the code a bit. Below is a small video of the setup:

 

 

The relay is in the bottom right hand corner of the picture underneath my hand. A simplified diagram of it is below.

 

 

 

 

 

 

 

 

 

 

 

 

To the extreme right you see the coil, which when energised will become an electromagnet and thus switch the contacts. On either side there is an SPDT switch, with one contact open and one closed depending whether the coil is energised or not. Thus this is a DPDT relay.

 

We need to wire this up correctly for the thing to work. There are several ways, and here is one of them:

 

Snubber Diode

The colour key above:

 

      The input from pin 5V of the Arduino board.

 

      The output to the leads of the motors. Doesn't matter which way round at first, you can swap them over            

       later if needs be.

 

      The input from pin 3 of the Arduino board, a PWM input.

 

      Ground (GND) which goes via an NPN transistor controlled at the base from pin 2 of your Arduino via a 2k2 resistor.

 

Don't forget the snubber diode. It's there to stop damage to the transistor.. The joystick wiring is just for the horizontal part as that's all we are using, and just the same as it was in the beginning of the chapter. Now we need a sketch for the Arduino:

 

int motorspeed;

void setup(){

  pinMode (2,OUTPUT);

pinMode (3,OUTPUT);}

 

void loop(){

 

if(analogRead(A1)>495&&analogRead(A1)<510){digitalWrite(2,0);}

if (analogRead(A1)>520){

digitalWrite(2,1);

motorspeed=analogRead(A1);

motorspeed=map(motorspeed,520,1023,50,254);}

if (analogRead(A1)<490){

motorspeed=analogRead(A1);

motorspeed=map(motorspeed,492,0,0,254);}

analogWrite(3,motorspeed);

 

}

 

 

 

 

And cantering quickly through, just before the setup we declare motorspeed as a global variable. We don't need to, we could just have it as a local to the loop, but I like to have globals in case I need to develop and the space isn't tight for memory so we can afford it. In the setup we set pins 2 and 3 to output mode, and the first thing we do in the loop is check where the joystick is by reading the output. between 495 and 510 means it's somewhere centre, so we set the relay to be unenergised.This excludes the motor from running in one direction, and we map to stop it running in the other:

 

motorspeed=map(motorspeed,492,0,0,254);}

 

This map also controls the speed in one direction (when the joystick is moved left) and that's why the range is backwards. The other way, the range is perfectly normal:

 

motorspeed=map(motorspeed,520,1023,50,254);

 

There are then two conditionals; either one can activate and not the other, or neither can activate- they cannot both activate at the same time. Then it's just a simple matter of outputting from pin 3 a PWM signal:

 

analogWrite(3,motorspeed);

 

and that's it.

 

Your relay may offer some resistance and so use a motor designed for 5V and not 6, otherwise there may not be enough juice to overcome the inertia. If you are suffering from this, wiggle the joystick full one way and then full the other.

 

 

Ian Lang Jan 2012