Home Chapter 13 BS2 Driving the Motor at Various Distances Bi- Directionally
BS2 Driving the Motor at Various Distances Bi- Directionally

Program 2: Driving the Motor at Various Distances Bi- Directionally

Enter the code below

' {$STAMP BS2}
' {$PBASIC 2.5}


Start: 'the starting point of program loop

Low 1 'turns motor counterclockwise
High 0
Pause 2000

Low 1 'turns motor off
Low 0
Pause 300

Low 0 'turns motor clockwise
High 1
Pause 500

Low 1 'turns motor off
Low 0
Pause 300

Goto start 'continue program at label “start”


Program 3: Driving the Motor with PWM

Enter the code below:

' {$STAMP BS2}
' {$PBASIC 2.5}

X VAR Byte 'creates variable "x" used to remember PWM voltage level


LOW 1 'turns motor off



FOR x=0 TO 255 'PWM increases to accelerate motor

PWM 1,x, 10


HIGH 1 'turn motor at full speed for short time

PAUSE 1000

LOW 1 'PWM decreases to decelerate motor

FOR x=255 TO 0

PWM 1,x, 10


GOTO start:

Introducing a New Part: Hall effect Sensor

Using the H-Bridge motor control permits you to control the speed and direction of the motor, however we do not really have a way of knowing how many times we have turned the motor with this circuit.

In this next experiment you will be using a Hall effect sensor to give the BS2 some feedback to count the number of times the motor has turned past a point in a period of time.


Ratiometric hall effect sensor


Hall effect sensors are valuable sensors because they do not have to make contract with the thing being sensed. These ratiometric Hall effect sensors are activated with the flux of a magnetic field. As the magnet comes into proximity with the sensor, it triggers the sensor and provides a linear output proportional to the distance that the magnet is from the sensor. There are digital Hall effect sensors as well that only produce an on or off state when triggered by a magnet.

Hall effect sensors are small and this allows amazing flexibility in how they are implemented. They are used under seats in newer cars to adjust the distance of the seat so the air bag can inflate more –or less in relation to the driver’s closeness to the steering wheel or dashboard. This allows the systems to sense small children who may not need the airbag inflated so vigorously, as well as large adults who may.

You will be using the A3515 Alegro ratiometric Hall effect sensor as a position indicator to give feedback to count the number of times the magnet attached to the shaft passes the sensor.


A3515 ratiometric sensor will work with as little as a 4.5v and a maximum supply voltage of 8v . It can provide 10 mA , so you can use this to input into the Basic Stamp.

NOTE: When using Hall effect sensors you must be careful not to use certain kinds of glues like cyanoacrylate (Superglue) to glue the sensor down to an object, as this will stress the surface of the hall effect sensor and can damage the sensor.

Building the circuit:

Parts necessary

3515 ratiometric Hall effect sensor

Connector wires as needed
(1) Rare earth magnet
(1) 10 k ohm resistor
(1) 220 k ohm resistor
(1) .01 µ f capacitor (104)

Use the schematic sheet below to add the elements in your BS2 breadboard as in the 3D models provided.






3D model of hall effect sensor and motor


Notice on the 3D models  that the rare earth magnet is attached to the shaft of the motor. This is placed under the Hall Effect sensor and used to sense the rotation of the motor. Be careful not to let this magnet accidentally get attached to the metal parts of your circuit board.

Hall effect sensor and motor driver