Programmable
Control
Why?
Many electronic devices on the market, are now designed using components that can be programmed to
function in different ways. The advantage is that the same key component used in one product can also
be used in something completely different.
Success Criteria
Use traffic lighting to record your progress. This will also help with revision as you will be able to see
what areas you need to work on. Your teacher will sign off this page after speaking to you about your self
evaluation:
Red: I need to work on this topic further to understand it.
Amber: I need a bit of help to fully understand this topic.
Green: I am confident in my understanding of this topic.
I can explain what a microcontroller is, how it works and can give examples of where they
are used
I can state the advantages & disadvantages of using control systems in industry.
I can explain the following terms; RAM, ROM, ALU, EEPROM, I/O port, bus and clock.
I can use and understand binary.
I can convert binary to decimal and vice versa.
I can identify and use various input and output transducer devices.
I can use correct symbols (start, stop, input, output, branch, loop) to construct flowcharts
showing solutions to simple control programs, involving time delays, continuous and fixed
loops
2
I can use suitable commands, including high, low, for…next, if…then, pause, end (or their
equivalents to construct programs to solve simple control problems, involving time delays,
continuous and fixed loops
I can use the computer and stamp controller to test my programs.
To access video clips that will help on this course
go to [Link]/MacBeathsTech
Electronic Control Systems
Many electronic devices have been developed to make life easier, safer, to help with work and for
entertainment purposes.
Some devices are purely electronic devices (for example a digital watch) however many of these devices
also control mechanisms (for example the eject mechanism in a video recorder) and so can be described
as mechatronic devices.
Both electronic and mechatronic devices all have one thing in common they are all electronic control
systems.
System diagram
Input Process Output
A system diagram is a more detailed block diagram that also shows the real world input signal (for ex-
ample light and heat) and the real world output signals (for examples movement or sound).
Example - The system diagram for a warning device for a freezer in a restaurant would be drawn as
shown below:
Heat Temp Sensor Driver Buzzer Sound
Or to put it another way...
INPUT INPUT OUTPUT OUTPUT
TRANSDUCER PROCESS TRANSDUCER
Input transducers are electronic devices that detect changes in the ‘real world’ and send signals into
the process block of the electronic system.
Output transducers are electronic devices that can be switched on and off by the process block of the
electronic system.
3
The T4 Controller
The ‘T4’ software runs on a computer and allows you to use the computer keyboard to type in pro-
grams.
The T4controller ‘runs’ programs that have been downloaded to it. It has indicator L.E.D.s to show
which inputs and outputs are off or on, and has connectors for the input and output modules.
The ’brain’ of the T4 controller board is the 18-pin microcontroller chip in the centre of the board.
Micro controller
A micro controller is often described as a ‘computer on a chip’.
Microcontrollers have a controller and memory all built into a single chip. As they are small,
inexpensive they can easily be built into other devices to make these products more intelligent and
easier to use.
Microcontrollers are usually programmed for a specific electronic product – for instance, a microwave
oven may use a single microcontroller to process information for all its electronic devices.
By altering the microcontroller program the same ‘brand’ of chip can do many different tasks.
Advantages of using microcontrollers in a product design are:
Increased reliability and reduced quantity of stock (as one microcontroller replaces several parts).
Simplified product assembly and smaller end products.
Greater product flexibility and adaptability since features are programmed into the
microcontroller and not built into the electronic hardware.
Rapid product changes or development by changing the program and not the electronic hardware.
4
Inside a Microcontroller
The ‘brain’ of the stamp controller system is the 18 pin micro controller in the centre of the board.
Although micro controllers are relatively cheap, they are very complex devices containing many
thousands of transistors, resistors and other electronic components.
The main features of the micro controller are shown in the block diagram;
ROM
The ROM (read only memory) contains the operating instructions for the micro controller. The ROM is
‘programmed’ before the micro controller is installed in the target system, and the memory retains the
information even when the power is removed.
RAM
The Ram (random access memory) is ‘temporary’ memory used for storing information whilst the
program is running. This is normally used to store mathematical answers that the micro controller
comes out with as it is working. This memory is ‘volatile’, which means that as soon as the power is
disconnected the contents of the memory are lost.
ALU
The processing unit (full name arithmetic and logic unit – ALU) is the ‘control centre’ of the micro
controller. It operates by reading instructions from the ROM and then carrying out the mathematical
operations for each instruction.
Clock
The clock circuit controls the speed at which these operations occur.
The clock circuit within the micro controller ‘synchronises’ all the internal blocks (ALU, ROM, RAM, etc)
so that the whole system works correctly.
Buses
Information is carried between the various blocks of the micro controller along ‘groups’ of wires called
buses. The ‘data bus’ carries data between ALU and RAM and the ‘program bus’ carries the program
instructions from the ROM to the ALU.
5
EEPROM
EEPROM (Electrically Erasable Programmable Read Only Memory) is a type of memory that can be
reprogrammed when desired, but it also keeps the program when the power supply is removed. This
means the stamp controller will start to run the program currently in the memory whenever the power
supply is connected.
[Link]
Flowcharts
Flowcharts are commonly used to explain how a program works. As flowcharts are drawn graphically
they often make programs easier to understand. A flowchart should be drawn for each program you
develop.
Start/stop symbol
The start/stop symbol shape is a rectangle with rounded ends. Each flow chart must contain one start
and usually one stop symbol unless it is a continuous loop.
Start/Stop
Wait symbol
The ‘wait’ symbol is a rectangle. The text inside the symbol explains how long the time delay is.
Wait x seconds
Outputs symbol
The ‘outputs’ symbol is a parallelogram. The text inside the symbol explains which output pins are
switched on or off at any time.
Switch on…
Decision Box
The Decision box is a diamond. The program uses this to check if something has been completed.
Repeated x
times? No
Yes
6
Continuous loop
Sometimes it is necessary to create programs that loop’ forever’, as is the case with traffic lights. There
is no ‘stop’ symbol because the program never ends!
Example Start
Red Light
ON
Wait 6 secs
Red Light
OFF
Green
Light ON
Wait 6 secs
Yellow
Light ON
Wait 2 Secs
Green Light
OFF
Yellow Light
OFF
Wait 1 Sec
7
Task 1 Task 2
Write out the flowchart for making a cup of tea. Set of temporary traffic lights is required for a
system of road works.
Red 10s
Red and Amber 2s
Green 10s
Amber 2s
Draw a flow chart for the lights sequence shown
above by one set of traffic lights. Use the times
given in the table.
8
Task 3
A microwave operates with the following sequence.
1. Light on
Draw a flowchart for this sequence. 2. Turntable on
3. Magnetron on
4. Wait 30s
5. Magnetron off
6. Wait 10s
7. Turntable off
8. Buzzer on
9. Wait 0.5s
10. Buzzer off
11. Light off
9
Writing a programme
Once a flowchart has been drawn it is necessary to convert it into the stamp controller programming
language, which is called PBASIC
A PBASIC program for the flowchart shown on the previous page is:
main:
high 7 ‘ switch pin 7 (red) on
pause 6000 ‘ wait for 6seconds
low 7 ‘ switch pin 7(red) off
high 5 ‘switch pin 59(green) on
pause 6000 ‘ wait for 6 seconds
high 6 ‘ switch pin 6(yellow) on
pause 2000 ‘ wait for 2 seconds
low 5 ‘ switch pin 5(green) off
low 6 ‘ switch pin 6 (yellow) off
goto main ‘return to start
end ‘ end the program
An explanation after the apostrophe (') symbol makes each line of a PBASIC program much easier to
understand. These comments are ignored by the computer when it downloads a program to the stamp
controller.
A label (for example ‘main:’ in the program above) can be any word (apart from keywords such as
‘high’), but it must begin with a letter. When the label is first defined it must end with a colon (:). The
colon ‘tells’ the computer that the word is a new label.
Task 4
Key in the program above into PICAXE and test.
Would this program be suitable for a traffic light system?
If not how could you improve it?
Test your new design in PICAXE
10
Symbols and Labels
When writing the computer programs for our flowcharts it can sometimes be hard to remember which
pins are connected to which devices. The ‘symbol’ command can then be used at the start of a program
to rename the inputs and outputs.
In a normal program there will be a ‘main program’, which includes many ‘mini programs’.
Mini programs’ or sub procedures are often used to separate the program into small sections to make
it easier to understand. Sub-procedures that complete common tasks can also be copied from program
to program to save time.
Example
symbol red = ouput 7 ‘rename output 7 red
symbol buzzer = output6 ‘rename output 6 buzzer
symbol counter = b0 ‘define a counter using variable b0
main:
gosub flash ‘make label called main
gosub noise ‘call the sub-procedure flash
goto main ‘loop back
end ‘end of the main program
flash: ‘make a sub-procedure called flash
for counter = 1 to 25 ‘start a for ….next loop
switch on red ‘red LED on
pause 50 ‘wait 0.05 seconds
switch off red ‘red LED off
pause 50 ‘wait 0.05 seconds
next counter ‘next loop
return ‘return from the sub-procedure
noise: ‘make a sub-procedure called noise
switch on buzzer ‘buzzer on
wait 2 ‘wait 2 seconds
switch off buzzer ‘buzzer off
return ‘return from the sub procedure
11
For … next’ loop
It is often useful to repeat the same part of a program a number of times, for instance when flashing an
LED. In these cases a ‘for … next’ loop can be used.
In this flowchart the LED connected to output pin 7 is flashed on and off five times. The number of times
the code has been repeated is stored - that is, stored in the RAM memory of the stamp controller. There
are 10 available variables, labelled b0 to b9, which can be used in this way. These variables can also be
renamed using the symbol command to make them easier to remember.
START
SET COUNTER =
SWITCH PIN 7
LOW
WAIT 1
SWITCH PIN 7
LOW
WAIT 1
HAVE WE
LOOPED 5
TIMES?
STOP
Task 5
Key in, download and run the following program.
symbol counter = b0 ' define the variable ‘counter’
symbol red = 7 ' define pin 7 with the name ‘red’
main: for counter = 1 to 5 ' start a for … next loop
high red ' switch pin 7 high
pause 1000 ' wait for 1 second
low red ' switch pin 7 low
pause 1000 ' wait for 1 second
next counter ' end of for … next loop
end ' end program
12
Connecting output transducers
The stamp controller can only drive low-power devices, such as LEDs, directly. It cannot drive devices
such as lamps, buzzers, solenoids or motors directly because these devices require a higher current to
operate.
A common way to drive these devices is with a transistor, as shown in the diagram above. In this case the
lamp is controlled by the transistor switching on and off.
The Output Driver
The output driver module provides four transistor outputs, as in the circuit shown above. Instead of using
four separate transistors, the output driver uses an integrated circuit called the ULN2803A, which
contains all the transistors in one 18-pin ‘chip’.
To use the transistor outputs, the output device should be connected between the screw-terminal
numbered output (4–7) and a V+ connection. The positive (red) wire on polarised devices (for example a
buzzer) should be connected to the V+ connection.
The white 6-pin header beside the screw terminals allows a ‘stepper motor’ or control model to be
connected easily to all four of the outputs.
13
The Motor Driver
The output driver module also contains a second integrated circuit called the L293D push-pull driver.
This chip allows forward and reverse control of two d.c. motors. Each motor output uses two of the
stamp controller output pins to control the direction of rotation of the motor.
Pin 4 Pin 5 Motor A Pin 6 Pin 7 Motor B
off off halt off off halt
off on forwards off on forwards
on off backwards on off backwards
on on halt on on halt
To use the push-pull motor output, the motor should be connected between the screw terminals
labelled A or B. If the motor turns in the opposite direction to that expected, the two motor wires
should be swapped over.
OUTPUT
STAMP
DRIVER
CONTROLLER
MODULE
14
Speed Control of D.C. Motors
There are two ways to control the speed of a D.C. motor. The simplest is to vary the voltage applied to
the motor. If, for instance, 3 V is applied to a small D.C. motor it will rotate at a lower speed than if 5 V
were applied. Unfortunately the ‘turning power’ (torque) of the motor will also drop, which means the
whole motor system will be less powerful.
The second way to control the motor is to always apply the full voltage (for example 5 V) across the
motor, but then to switch the power supply on and off rapidly. As the power supply is off some of the
time, the motor does not receive as much power and so the motor turns more slowly. The advantage of
this system is that the torque remains quite high.
This system is called pulse-width modulation (PWM). The time that the power supply is switched on is
called the mark time, and the time that the motor is switched off is called the space time. By varying the
on (mark)-to-off (space) ratio, the speed of the motor can be varied.
MARK
Voltage
0
SPACE Time
15
Binary Numbers
The counting system used in everyday activities is the decimal system. This number system uses the ten
familiar digits 0 to 9 to explain how big or small the number is.
However, as the microcontroller only recognises the two electronic states high and low, it uses the
binary number system. This number system uses just two digits, 0 and 1. An electronic signal that is low
is represented by ‘logic 0’, and a signal that is high is represented by ‘logic 1’.
Eight bits grouped together are described as a byte. The decimal value of a byte is calculated by adding
together the corresponding decimal value of each of the individual bits. The eight bits in a byte are
labelled bits 0 to 7, from right to left. The rightmost bit is called the least significant bit (LSB) and the
leftmost bit is called the most significant bit (MSB). The decimal value of each bit is given in the table
below.
bit number 7 6 5 4 3 2 1 0
decimal value 128 64 32 16 8 4 2 1
The binary number %10010111 when converted into decimal would be:
bit number 7 6 5 4 3 2 1 0
decimal value 128 64 32 16 8 4 2 1
10010111 = 128 + 16 + 4 + 2 + 1
= 151
Task 16 Task 17
Convert each of the following binary Convert each of these decimal numbers
numbers into decimal. into binary.
a)%11110000 a)17
b)%11000011 b)23
c)%01010101 c)11
d)%10101010 d)38
e)%00001001 e)33
f)%11111010 f)137
g)%10101011 g)46
h)%00010000 h)147
16
Task 6
Connect 2 motors to the output driver module as shown in the diagram.
The ‘high’ and ‘low’ commands can be used to switch the output pins and control the motors forward,
backward and halt.
main: ' make a label called ‘main’
high 5 ' motor A forward
high 7 ' motor B forward
pause 1000 ' wait 1 second
OUTPUT
low 5 ' motor A halt STAMP
DRIVER
CONTROLLER
low 7 ' motor B halt MODULE
pause 1000 ' wait 1 second
high 4 ' motor A backward
high 6 ' motor B backward
pause 1000 ' wait 1 second
low 4 ' motor A halt
low 6 ' motor B halt
goto main ' jump back to the start
Key-in the program listed above, and then download it to the stamp controller. The motors should start
moving.
Task 7
Connect a d.c. solar motor across the ‘V+’ and ‘7’ terminals on the output driver module. This will
provide an interfacing circuit as shown.
a) Key in, download and run the program listed below. This program drives the motor at approximate-
ly half speed, as the space (off time) is twice the length of the mark (on time). Note that you must use
pause delays, as the power supply must switch on and off very quickly (wait delays would be too long).
main: ' make a label called ‘main’
high 7 ' output high
pause 5 ' wait 5 ms
low 7 ' output low
pause 10 ' wait 10 ms
goto main ' jump back to the start
b) Try out different speeds (by experimenting) by altering the length of the pause delays.
c) Describe the advantages and disadvantages of using PWM speed control.
17
The Input Module
The input module provides the interfacing circuits required to connect
switches and sensors to the stamp controller.
When the slide switch on the input module is ‘up’ the input module provides
four digital (on/off) switch connections. These can be used to connect input
switches (for example a microswitch) to the stamp controller. The switches
can be connected through the screw-terminal blocks or through the white
push-on headers.
Input pins 0 and 1 have ‘on-board’ test switches. These allow programs to be
tested without the need to connect external switches.
Digital sensors
A digital sensor is a simple ‘switch’ type sensor that can only be ‘on’ or ‘off’.
5V
VOLTAGE
0V
TIME
Common examples of a digital sensor are:
microswitches
push-and-rocker switches
reed switches.
18
Task 8
Key in, download and run the program listed below. This program makes output pin 7 flash every time
the push-switch on input pin 0 is pushed.
main: ' make a label called ‘main’
if pin0 =1 then flash ' jump if the input is on
goto main ' else loop back around
flash: ' make a label called ‘flash’
high 7 ' switch output 7 on
pause 2000 ' wait 2 seconds
low 7 ' switch output 7 off
goto main ' jump back to start
In this program the first three lines make up a continuous loop. If the input is off the program just loops
around time and time again.
If the switch is then pushed, the program jumps to the label called ‘flash’. The program then switches
pin 7 on for two seconds before returning to the main loop.
Note carefully the spelling in the ‘if … then’ line – ‘pin0’ is all one word (without a space). Note also
that only the label is placed after the command ‘then’ – no other words apart from a label are allowed.
Task 9
A burglar alarm must sound a buzzer and light a warning signal for 20 seconds when any of the four
windows in a house are opened. Each window contains a reed switch that is connected to the alarm.
Draw a flowchart and write a PBASIC program that will operate the burglar alarm correctly. Use the
following input and output connections.
Input connection Pin Output connection
7 red light
6 buzzer
5
4
switch 3 3
switch 2 2
switch 1 1
switch 0 0 [Link]
19
Task 10
As part of a Christmas decoration in a shop, a lighting sequence is to be controlled by a microcontroller.
The output connections are shown below.
Input connection Pin Output connection
7 red light
6 yellow light
5 green light
4
[Link]
3
2
1
pressure mat 0
When a visitor treads on a pressure mat under the carpet, the lights should flash on and off in sequence
three times.
The following PBASIC program will carry out the instructions shown in the table above. Copy and then
complete the program listing by adding the missing comments.
symbol counter = b0 '…
symbol red = 7 '…
symbol yellow = 6 '…
symbol green = 5 '…
main: if pin0 = 1 then flash '…
goto main '…
flash: for counter = 1 to 3 '…
high red '…
low green '…
pause 500 '…
high yellow '…
low red '…
pause 500 '…
high green '…
low yellow '…
pause 500 '…
next counter '…
low green '…
goto main '…
20
Task 11
Develop a PBASIC program that will carry out the instructions shown in the flowchart above. Use the
following pin configuration.
Input connection Pin Output connection
7 red light
6 amber light
5 green light
4
3
2
1
start switch 0
START
GREEN
LED ON
SWITCH
N
PUSHED?
GREEN LED OFF
AMBER LED ON
WAIT 3 s
GREEN LED OFF
RED LED ON
WAIT 4 s
AMBER LED
ON
WAIT 2 s
RED/AMBER LED
OFF
GREEN LED ON
[Link]
21
Analogue Sensors
An analogue sensor measures a continuous property such as light, temperature or position. The analogue
sensor provides a varying voltage signal. This voltage signal can be represented by a number in the range
1 to 240 (for example very dark = 1, bright light = 240).
VOLTAGE
LIGHT
5V
DARK
0V
TIME
The input module provides the interfacing circuits required to connect switches and sensors to the stamp
controller.
When the slide switch is ‘down’ the input module provides two digital (on/off) and two analogue sensor
connections.
Common examples of analogue sensor are:
LDR (light sensor)
Thermistors (temperature sensor)
Variable resistor (potentiometer).
22
Task 12
Key in, download and run the program listed below. This program makes a red or green LED light
depending on the light level falling on an LDR sensor. Before running this program make sure you have
connected a light sensor to the input module and that the slide switch is in the ‘down’ position.
main: ' make a label called ‘main’
if sensorA > 50 then red ' jump if the value > 50
goto green ' value is < 50 so jump
red: ' make a label called ‘red’
high 4 ' switch output 4 on
low 5 ' switch output 5 off
goto main ' jump back to start
green: ' make a label called ‘green’
high 5 ' switch output 5 on
low 4 ' switch output 4 off
goto main ' jump back to start
In this program the second line checks the value of sensor A. If the value is bigger than 50 the program
jumps to label red.
If the value is not bigger than 50 the next ‘goto green’ line is carried out instead.
Note that only the label is placed after the command ‘then’ in the ‘if … then’ – no other words apart
from a label are allowed.
Task 13
Key in, download and run the program listed below. This program gives the variable b0 the value of the
temperature sensor (sensor A). The debug command then sends this value to the computer screen so
that the temperature level can be seen.
Before running this program make sure you have connected a temperature sensor to the input module
(sensor A) and that the slide switch is in the ‘down’ position and you have selected ‘Extended PBASIC’
from the options menu.
main: ' make a label called ‘main’
let b0 = sensorA ' give b0 the value of the sensor
debug b0 ' show the value on the computer
pause 10 ' wait 0.01 second
goto main ' jump back to start
23
Task 14
A street light must turn on when the light level is below the level ‘100’ and turn off when the level is
above ‘100’. The lamp is connected to output 7 and the LDR light sensor is connected to sensor A.
Draw a flowchart and write a PBASIC program that will make the street lamp work correctly.
Task 15
For each input transducer state whether they are an analogue or digital sensor.
(a) LDR (f) tilt switch
(b) reed switch (g) push switch
(c) microswitch (h) rocker switch
(d) thermistor
(e) variable resistor
24