Experiment #4: Simple Automation #4: Simple Automation Page 58 • “What’s a Microcontroller?”...

14
Experiment #4: Simple Automation What’s a Microcontroller?” Student Guide Version 1.9 Page 57 Experiment #4: Simple Automation In Experiment #3 we used a servo (a specialized type of motor) to demonstrate how a microcontroller can manipulate a mechanical device in the “real world”. The program that we wrote (and downloaded to the BASIC Stamp’s memory) controlled the rotation and position of the servo. The program caused the servo to swing back and forth between the servo’s two different extremes of rotation. This was an example of how a microcontroller can cause a motive device to operate. However in Experiment #3, the BASIC Stamp was “blind”. All the servo did was respond to our code. Remember that the very heart of a microcontroller is its ability to make decisions based on inputs and then manipulate the “real world” with outputs. In this experiment we’re going to do just that. Yes, we’re going to move the servo again, but only if the proper input conditions are met. You could think of this experiment as the small equivalent of an automatic door at the supermarket. The door is closed most of the time until somebody – or something – comes near, then the door automatically opens. There is apparently nothing that we need to do in order for the door to open. We’re not pushing any buttons, just by being near the door is enough to cause it to open. This is a very basic form of automation. Some of the sensors that are used for this type of application are quite sophisticated, and others are quite simple. However, they all have one thing in common and that is that they sense an input and deliver the signal to a microcontroller so that it can make a decision, in this case “opening the door”. As our “detection sensor”, we’re going to use a device called a “photoresistor”. It’s a device designed to detect different light levels. It’s a type of “optical” sensor, a part that is classified as an optoelectronic device. Let’s automate! Experiment #4 requires the following parts: Automation: In this experiment, the term automation means that something is being done, without any “human interaction”. In our example (the supermarket automatic door), this isn’t exactly true. Although we’re not physically pushing any buttons, we are, by our presence, figuratively “pushing the light detectors button”. This however, appears to be completely automatic, since we don’t have to “think” about doing any thing other than walking up to the door. In its truest sense, aut omation is the microcontrollers ability to make things happen with no interaction on our part.

Transcript of Experiment #4: Simple Automation #4: Simple Automation Page 58 • “What’s a Microcontroller?”...

Experiment #4: Simple Automation

“What’s a Microcontroller?” Student Guide Version 1.9 • Page 57

Experiment #4: Simple Automation

In Experiment #3 we used a servo (a specialized type of motor) to demonstrate how a microcontroller can manipulate a mechanical device in the “real world”. The program that we wrote (and downloaded to the BASIC Stamp’s memory) controlled the rotation and position of the servo.

The program caused the servo to swing back and forth between the servo’s two different extremes of rotation. This was an example of how a microcontroller can cause a motive device to operate. However in Experiment #3, the BASIC Stamp was “blind”. All the servo did was respond to our code. Remember that the very heart of a microcontroller is its ability to make decisions based on inputs and then manipulate the “real world” with outputs. In this experiment we’re going to do just that. Yes, we’re going to move the servo again, but only if the proper input conditions are met. You could think of this experiment as the small equivalent of an automatic door at the supermarket. The door is closed most of the time until somebody – or something – comes near, then the door automatically opens. There is apparently nothing that we need to do in order for the door to open. We’re not pushing any buttons, just by being near the door is enough to cause it to open. This is a very basic form of automation. Some of the sensors that are used for this type of application are quite sophisticated, and others are quite simple. However, they all have one thing in common and that is that they sense an input and deliver the signal to a microcontroller so that it can make a decision, in this case “opening the door”. As our “detection sensor”, we’re going to use a device called a “photoresistor”. It’s a device designed to detect different light levels. It’s a type of “optical” sensor, a part that is classified as an optoelectronic device.

Let’s automate! Experiment #4 requires the following parts:

Automation: In this experiment, the term automation means that something is being done, without any “human interaction”. In our example (the supermarket automatic door), this isn’t exactly true. Although we’re not physically pushing any buttons, we are, by our presence, figuratively “pushing the light detectors button”. This however, appears to be completely automatic, since we don’t have to “think” about doing any thing other than walking up to the door. In its truest sense, automation is the microcontrollers ability to make things happen with no interaction on our part.

Experiment #4: Simple Automation

Page 58 • “What’s a Microcontroller?” Student Guide Version 1.9

Parts Required

Build It!

For this experiment you will need the following:

(1) BASIC Stamp II (1)“Board of Education” (1) Three pin connector (if using Rev. A Board of Education) (1) Programming Cable (1) R/C servo (1) LED (1) 470 ohm, ¼ watt resistor (1) 3300 microfarad electrolytic capacitor (if you are using a Rev. A Board of Education) (1) Photoresistor, Cadmium Photocell (CdS) (1) 10K ohm, ¼ watt resistor (1) 9 volt battery or wall transformer (misc.) connecting wires (1) Personal Computer running DOS 2.0 or greater, with an available serial port. (1) BASIC Stamp Editor program

Using the Board of Education, create the hardware circuit as shown in the figures below. Figure 4.1 is the schematic and Figure 4.2 is the pictorial (what the circuit physically looks like).

Depending on which model of servo you have, the color coding on the wires may vary. In all cases (with the servos you get from Parallax), the black wire is connected to Vss and the red wire is connected to Vdd. The remaining (third) wire may be white or yellow (or something else). This is the control input wire which we’ll be conneting to the P1 signal on the BASIC Stamp.

Experiment #4: Simple Automation

“What’s a Microcontroller?” Student Guide Version 1.9 • Page 59

Figure 4.1: Schematic for Experiment #4 on a Rev B Board of Education: Note: Rev. A Board of Educations require the 3300 uF capacitor across the Vdd and Vdd. This schematic is for the Rev B. Board of Education.

LED

470 W

Photocell

P7

Vdd

10 kW

Vdd

P5

Red

BlackServo

White

Vdd

Vss

Vss

P123300 µF

Vss

Vdd

Capacitor Required onBoard of Education Rev A

Figure 4.2: Physical picture for of circuit for Rev. B Board of Educations

Experiment #4: Simple Automation

Page 60 • “What’s a Microcontroller?” Student Guide Version 1.9

Program It!

This circuit has two types of output devices (the servo and the LED) and one type of input device (the photoresistor). Remember to connect the LED properly!

Once you have all the components installed into the Board of Education’s prototype area (as shown in Figures 4.1 and 4.2) attach the programming cable from the Board of Education to your PC and connect either a 9 volt battery or a 9 volt DC wall transformer to the

Board. Since the servo requires a lot of current (much more than an LED), battery life will be quite limited, so use the transformer if you have one. Turn on your PC, and double click on the BASIC Stamp icon. You should now be running a program called the “Stamp Editor”. This is a program that was created to help you write and download programs to the BASIC Stamp microcontroller. Type in the following program: this_place: high 5 pause 200 low 5 pause 200 goto this_place Now while holding the “ALT” key down, type the letter “r” (for “run”) and press “enter”. Just another blinker program? Well yes and no. Notice that there is no “output 5” command in the program at all. If you’ve been reading through the appendices throughout these lessons you’ll discover the the “high” and “low” commands automatically make the pin an output.

Program space: Microcontrollers may have several types memory which they use to carry out their tasks. In the case of the BASIC Stamp 2, we’re limited to 2048 bytes of (EEPROM) memory storage. This amount of space is used for both program and data storage. If you write a program that automatically gathers data over a period of time (such as a remote weather station), you’ll want to make your program as small and as efficient as possible to allow as much room for data storage as you can.

EEPROM: This stands for ‘electrically erasable, programmable, read only memory. Although a sophisticated development in the "memory industry”, it’s really quite simple to use. We can store our programs & data in EEPROM with very simple commands. Then when the power is removed, both the program & data is retained. What sets the EEPROM apart from most other types of “solid state” memory is that it can be very easily erased (“automatically”) & re-written to, again and again.

Experiment #4: Simple Automation

“What’s a Microcontroller?” Student Guide Version 1.9 • Page 61

This saves some keystrokes, and more importantly saves program space on the BASIC Stamp. We really don’t need to worry about running out of program space with our (small) experiment programs, but as you begin to create larger and more complex programs, it’s a good habit to sharpen your programming skills to turn out “high quality” programs. Not only will you be less likely to run out of program space, but your code will actually run faster, resulting in a quicker execution time. Modify the program to look like this: n var bit n=0 this_place: n=0 low 5 debug ? n pause 1000 high 5 n=1 debug ? n pause 1000 goto this_place Run the program. Not only should your LED be blinking but you should also have an “information box” on your PC’s monitor, alternately displaying “n=1” and “n=0”. Let’s dissect, and see what’s going on… n var bit A variable called “n”, one bit in size. this_place: A label in the program n=0 Something new. We’re going to “set the value of ‘n’ to 0” low 5 Make P5 low, thereby turning on the LED.

Experiment #4: Simple Automation

Page 62 • “What’s a Microcontroller?” Student Guide Version 1.9

debug ? n Debug? That word sounds familiar. Remember that to “debug” means to remove the errors in your program. Well, the PBASIC language has a command called “debug”, which can really aid in getting rid of all those program glitches. Normally we send the program from our PC down the programming cable to the BASIC Stamp. It’s essentially been (until now) a one-way trip. Debug is a very specialized command that allows the BASIC Stamp to send information (“data”) back “up” the

cable, and display it on your PC’s monitor. In this manner we can “look inside the BASIC Stamp” and see the data that the BASIC Stamp is working with. In this case, we set the value of “n” to 0 in a previous command. When the debug command is encountered, it “prints” the value of “n” onto the PC’s debug window. The “?” is an abbreviation for “print”, so the command literally says: “Open the debug window on the PC and print the value of “n” on the screen”. pause 1000 Easy by now, right? high 5 Turn off the LED. n=1 Here we’re changing the value of “n” to 1. debug ? n We’re sending the value of “n” back to the PC. Since the value of “n” has changed, debug “prints” the change on your PC’s screen. pause 1000 Yep. We know what this does. goto this_place Go do it all over again. Now some of you may be thinking “Why do we need to see the value of “n” in our debug window? Since our program (that we wrote) is setting the value of “n”, we

already know what the value of “n” is Why bother displaying it back to us?

Hard-coded: A value or parameter that is set absolutely to a specific value. There are other ways to set values to a predetermined value (such as using the c o nc o n command), that make it easier to change their value in the future. In many cases the variables used in a program are dynamic in that they constantly change their value during program execution.

Debug (command): A very useful tool for “seeing” what your program is doing down inside the Stamp. The Debug command has a tremendous amount of flexibility built in. Check out the appendix for a complete description of each of the available features.

Experiment #4: Simple Automation

“What’s a Microcontroller?” Student Guide Version 1.9 • Page 63

That’s true, the value of “n”, in this example is “hard-coded” – the program sets the value with no variance. However, when a variable is set by external events, such as when an input changes due to a switch being pushed for example, then debug allows us to see the changing input data, and then ascertain whether or not our program is reacting properly. Let’s try it. Modify the program: n var bit this_place: n=in7 debug ? n pause 100 goto this_place Now instead of “hard-coding” the value of “n”, we’re allowing “n” to be equal to the value of whatever “in7” is. Since P7 is connected to our photo-resistor, when the amount of light that the photo-resistor sees changes, the input voltage (on P7) also changes. Move your hand over the photo-resistor (no need to touch, although you won’t hurt it). The debug value on your PC should be changing from a “1” to a “0”, depending on whether the photo-resistor sees a light or dark environment. Be sure the lights are on in your room. This part of the circuit is called a resistive divider, and we’ll explore this in greater detail in an upcoming experiment. Suffice it to say at this point, that as the photo-resistor changes its resistance (due to varying light levels), the voltage changes on P7. This voltage change is an analog signal. Now, since microcontroller input pins only recognize binary (digital) values, when the voltage reaches a certain point, the value that P7 sees will be either a “1” (+ 5 volts) or a “0” (0 volts). What we’ve just done is created a “switch” that doesn’t need to be pushed! It’s a sensor that reacts automatically to external light levels. Now let’s do something fun… Change the program to this: x var word n var bit output 12

Experiment #4: Simple Automation

Page 64 • “What’s a Microcontroller?” Student Guide Version 1.9

close_the_door: for x = 1 to 100 pulsout 12,500 pause 10 next pause 10 look_for_people: n=in7 if n = 1 then open_the_door pause 100 goto look_for_people open_the_door: for x = 1 to 100 pulsout 12,1000 pause 10 next pause 10 n=in7 if n = 0 then close_the_door goto open_the_door What do you think this program is going to do? Don’t be discouraged that the program may be getting a little longer. Let’s just break if down into manageable, byte sized chunks (pun!) This is the initialization part of the program, we already know what these three commands do. x var word n var bit output 12 We used this routine in Experiment #3. It sends out a series of pulses that makes the servo rotate to one end of its travel. close_the_door: for x = 1 to 100 pulsout 12,500 pause 10 next pause 200

Experiment #4: Simple Automation

“What’s a Microcontroller?” Student Guide Version 1.9 • Page 65

This part of the program simply looks at whether or not the photo-resistor detects a “shadow”, & if it does, causes the program to go to the routine that will “open the door”. look_for_people: n=in7 if n=1 then open_the_door pause 100 goto look_for_people If our servo was larger and connected to the supermarket’s front door, this routine would open it. open_the_door: for x = 1 to 100 pulsout 12,1000 pause 10 next pause 200 This is for “safety”. As long as the person is standing anywhere near the door, keep the door open. (You don’t want to crush too many customers, they’ll start shopping somewhere else!) n=in7 if n =0 then close_the_door goto open_the_door

Experiment #4: Simple Automation

Page 66 • “What’s a Microcontroller?” Student Guide Version 1.9

Questions

1. What is automation? 2. What does the “debug” command do, and why is it useful? 3. What does the command n = in7 do? How does the command is_person_there = in7 differ in

execution? 4. In this experiment, how does the microcontroller know when to “open the door”? 5. Add appropriate remarks to the following program: x var word ___________________________________ n var bit ___________________________________ output 1 ___________________________________ close_the_door: ___________________________________ for x = 1 to 100 ___________________________________ pulsout 12,500 ___________________________________ pause 10 ___________________________________ next ___________________________________ pause 200 ___________________________________ look_for_people: ___________________________________ n=in7 ___________________________________ if n=1 then open_the_door ___________________________________ pause 100 ___________________________________ goto look_for_people ___________________________________ open_the_door: ___________________________________ for x = 1 to 100 ___________________________________ pulsout 12,1000 ___________________________________ pause 10 ___________________________________ next ___________________________________ pause 200 ___________________________________ n=in7 ___________________________________

Experiment #4: Simple Automation

“What’s a Microcontroller?” Student Guide Version 1.9 • Page 67

Challenge!

if n = 0 then close_the_door ___________________________________ goto open_the_door ___________________________________

1. Write a program (complete with remarks) that will turn on the LED (on P5) every time the photo-resistor is in a shadow.

2. Write a program (with remarks) that will blink the LED twice and then “open the door” (rotate the servo),

when the sensor detects a shadow. Then recycle and do it again. 3. Write a program (with remarks) that will blink the LED twice and then “open the door (rotate the servo),

when the sensor detects a shadow. Then while the sensor is still detecting a shadow, blink the LED continuously, until the shadow goes away. Then recycle and do it again.

4. Write a program that will cause the LED to blink continuously, until a shadow is detected by the photo-

resistor. Once the shadow is detected, the LED is turned on while the “door is being opened”. Once the door is open, the LED is off, until the shadow goes away. Then recycle and do it again.

5. Think of conditions where the program for this experiment would not work correctly.

Experiment #4: Simple Automation

Page 68 • “What’s a Microcontroller?” Student Guide Version 1.9

What have I learned?

On the lines below, insert the appropriate words from the list on the left.

______________is a fascinating application for microcontrollers. Without any intentional ______________by humans, the BASIC Stamp can make things happen based solely on sensory inputs. The supermarket’s ______________door is a great example of a typical microcontroller application. There are many different types of sensors that can detect movement in the “real world”. In this experiment we used a ______________whose value is dependent upon how much light it detects. Since this sensor detects light (or its absence), it is sometimes referred to as an “______________” sensor. The “Debug” command allows the BASIC Stamp to send ______________back to the PC, so that we can determine how well our program is operating, and whether or not it’s making the appropriate “decisions”. In this experiment the BASIC Stamp sent the value of “n” back to the PC, which was assigned to the value of the ______________ input on P7. As we develop larger programs, it becomes increasingly important to write our “code” as ______________ as possible. This is important for several reasons. First, most microcontrollers (like the BASIC Stamp) have a limited amount of ______________ for program and data storage. The fewer instructions that we can use to accomplish a given task, means the more features that we can add to our program. Secondly, fewer instructions to accomplish a given task allows our program to ______________ faster – yielding a faster ______________time to “real world” situations. As an example, if our program was poorly written & the door didn’t open quickly, the customer might walk right into the door. Not exactly great customer relations!

automatic photo-resistor efficiently automation response input optical execute action data memory

Experiment #4: Simple Automation

“What’s a Microcontroller?” Student Guide Version 1.9 • Page 69

Why did I learn it?

How can I apply this?

Knowing how to “automate” certain tasks can take the drudgery (& sometimes danger) out of many types of jobs. Automation in the automobile industry has significantly improved many facets of the assembly process. Painting and welding, for example, are now done by automated robots with more consistency at lower cost and with less risk to employees.

On the down side, many people during this “automation era”, are being displaced. Retraining is necessary, but hopefully the next job they get won’t be nearly as hazardous or tedious. On the plus side, there is a great opportunity for new products and processes that are accomplished automatically. This has spawned a whole new type of “innovation industry”. No longer do you need to “put nut A on to bolt B”, hour after hour, day after day. Now you can use your imagination to develop new and ever changing products that help improve life for everyone.

There are many opportunities to improve what we might consider to be rather mundane tasks. For example, you could design a supermarket door that not only opened automatically, but also kept track of how many people actually entered (or for that matter, exited) the store.

You could keep track of this during specific time periods throughout the day, so that your microcontroller would alert the manager of the store that he’s going to need additional check-out clerks, because there’s more people shopping at this time. This would improve customer service because the employees wouldn’t be caught “off guard” with a bottleneck at the checkout stands. Your system would be a form of “shopping crowd early warning device”!

Experiment #4: Simple Automation

Page 70 • “What’s a Microcontroller?” Student Guide Version 1.9