MEAM 410/510 Mechatronics Programming/Electronics Lab I

Goal

The goal of the lab is become familiar with the BASIC Stamp 2 and the development. You will learn how to program the BASIC Stamp, interface it with a push button, measure resistances, write to a LED display and drive a speaker.

Pre-Laboratory Preparation

1. Read the lab write-up carefully so you know what you have to do when you walk into the lab.

2. Read pages 7-32, but don’t worry about the development board – we will use a different board in the lab. Focus on BASIC Stamp 2 only.

3. Look for descriptions for all the commands used in the write-up below so you understand what each command does and how to use the BASIC Stamp.

Procedures

1. Button Timer

Before programming the Stamp to do anything useful, let’s make sure that it’s working and we know how to write and run a simple BASIC program. First, the Stamp should be connected to a suitable power supply, the easiest way to do this is to plug in a 9V battery into the 9V battery terminal on the carrier board. If your doing a lot of development and testing, the best way is to connect a power supply (either a 5V or 5-15V) to the carrier board. A 5V supply can be connected to the 5V and ground terminals or a supply between 5V and 15V can be connected to the 5V-15V and ground terminals. All connections to the Stamp should be made by plugging connecting wires into the screw down terminals on the carrier board (with BASIC Stamp included) that you’ve been provided. Finally, the Stamp must be connected to the PC in order to download programs, this connection is made through the serial cable provided.

Now, we can write and execute programs on the Stamp using the editor/compiler “BASIC Stamp Editor v2.1” on the PC. It should be an icon located on the desktop or go to the “Start Menu” to locate it under “Programs”.

Make a new directory in a convenient location to save the programs that you are about to create. As the write-ups for this lab requires you to hand in code used in each section, be sure to remember to save a copy of all your code.

‘{STAMP BS2} ‘Initializes the type of Stamp used cnt var byte ‘Define the variable cnt as a type byte cnt = 0 ‘Initialize cnt to zero loop: ‘Start of loop cnt = cnt + 1 debug ? cnt ‘Display the value of cnt

1 goto loop ‘Go back to the beginning of the loop

This program can then be run by pressing “Alt-R” or by going through the “Run” menu. The statements following the single quote (“ ‘ ”) are comments, and need not be entered. The initialization statement in the first line is an exception and has to be included with the single quote.

 Describe the output of the program.  About how fast does the program count (i.e., how long does it take to count to 255)?  How does the type of the variable cnt affect the output of the program?

Next, we will look at interfacing the Stamp with external circuitry. As an example of the BASIC Stamp’s functionality, let’s suppose that we needed to know the length of time a push button is held down. Prepare the circuit according to Figure 1, such that when the button is held down, a 5V supply is connected to pin 3. When the push button is released, pin 3 will be grounded. Thus, when the button is held down for a period of time and then released, pin 3 will receive an input pulse of 5V.

Figure 1: Push Button Input Circuit

Next, display the state of pin 3 using the following program.

‘{STAMP BS2} value var word ‘Define a variable called value input 3 ‘Set pin 3 as an input loop: value = in3 ‘Read the value of pin 3 (0 or 1) debug ? value ‘Echo the value to the screen goto loop

The second line (input 3) sets pin 3 as an input pin (conversely, output 3 would make pin 3 an output pin). The line loop: simply acts as a location marker in the program and does nothing else. A location marker in general is the target location in the code of a goto command. The real action occurs in the next line, where the value of pin 3, either a 1 or a 0, is stored in the variable “value”, and then displayed.

Next, combine the two programs in order to count the amount of time between when the button is pressed and when it is released. You will need an if then statement, which has the standard syntax: “if value = 1 then place”, where place is the location marker for the if-then branch.

 How does this program work in practice?  Do you get values that make sense for the amount of time the button is held down?  Do you observe any strange behavior in the output?

2 Hook up the oscilloscope to the circuit in Figure 1 by connecting the scope probe to interconnection point between the pot, switch and BS2 I/O pin. Make sure the probe is grounded and observe the signal that arrives at the Stamp’s input pin when the button is pressed. To change the scale on the display use the “time/division” and “volts/division” knobs corresponding to the input channel that you’re using on the scope. Once you have a nice graph on the display, you can press the “run/stop” button to freeze the display.

 Describe the signal you observed. How much does the signal fluctuate? How long does it take for the oscillations to die down?  Based on earlier parts of this lab, explain why you did or did not see this effect on the output screen in your program.

The problem with reading in the length of time that a button is pushed is that in reality, the button contact experiences a very fast series of mechanical bounces (as you just saw on the oscilloscope) that introduces error into the measurements (see Figure 2). For this reason, the readings that one gets are often not accurate, since they reflect the time between bounces, and not the time between when the button is pushed and when it is released. There are several ways to “debounce” a push-button, in order to fix this problem. First, in (electronic) hardware, we can attempt to smooth the signal using a capacitor. Try connecting a capacitor (say 0.1 µF) in parallel with the resistor. Check the effect on the oscilloscope.

Figure 2: Switch Bounce

 Ho w

does the graph on the oscilloscope look different from before (i.e. without the capacitor)? Why?  Does it change the performance of the timer function? If so, how?  Why should this circuit help debounce the button (try to explain it using your knowledge of resistors and capacitors hooked together)? Describe the output on the oscilloscope with and without the capacitor in the circuit.  Approximately how many counts do you find the Stamp makes per second?

Alternatively, we can debounce the circuit in software. One way to do this is to take two measurements, separated by a time interval longer than the expected time of the mechanical bounces. That is, we measure for the button being pushed, and then check again after a small period of time to see that it is still pressed. This is essentially what is done in the Stamp’s button command. Although we will not use it here, you should refer to the Stamp’s manual for a detailed description of the command. You might find it useful for the final project.

3 2. Reading Resistances

The Stamp provides a built-in function for reading resistances from 5 to 50kΩ in a passive circuit. In the BASIC Stamp I, this is called the POT (short for “potentiometer”) command. A modified (and enhanced) version of this command is given by the RCTIME command for the BASIC Stamp II. To summarize, the RCTIME command measures the RC discharge time of a network connected to one of the Stamp’s input pins. If one knows the capacitance value, this gives a means of determining the value of the resistance. For our purposes, we will use a 0.1μF capacitor that should be placed between the +5V and the input pin used to measure the resistance. A sketch of the setup is given in Figure 3.

Figure 3: Potentiometer Input Circuit

Below is a sample program to store and display the value of a potentiometer:

‘{STAMP BS2} result var word ‘variable to hold result loop: high 7 ‘discharging the cap pause 1 ‘pause for at least 1ms RCTIME 7,1,result ‘measure RC charge time debug ? result ‘show value on screen goto loop

In the RCTIME command, the Stamp reads pin 7 and stores the discharge time in the variable “result”. Notice that pin 7 must be held high for 1ms to start the loop. The reason for this is described in more detail in the description of the RCTIME command in the manual for the Stamp.

Test out this feature by measuring the resistance of several individual resistors. Use this to derive a rough calibration for the measurements (i.e. make a plot of RC discharge time versus known resistance). Then try it on one of the potentiometers provided by connecting the middle lead on the potentiometer to the input pin on the Stamp (and to +5V through a 0.1μF capacitor, see Figure 3), and either of the other leads to ground.

 What happens as you vary the potentiometer?  How close is your calibration to the resistance of the potentiometer? Set the potentiometer to a certain value and read the output to determine the resistance. Then take the potentiometer out of the circuit and measure its resistance manually using the multi-meter.

3. LED Display

The 10-segment LED display provides a very simple means of communicating data back to the user. Each LED is turned on when current flows across it (it is biased to only work in one direction, so you will need to figure out which direction this is, and you should mark it on the LED block). Since the LED is just a

4 (light emitting) diode, it offers very little resistance. If we connect a power supply directly across it, the resulting current will be very large and will likely burn out the LED (not a good thing). To protect the LED, we connect it to the Stamp through a resistor as shown in Figure 4:

Figure 4: LED Circuit

You should test the LEDs with the power supply just to make sure they work (don’t forget the resistors!).

 Given the schematic in Figure 4, for what setting of the output pin will the LED be on or off? Why?

Next, build an LED display that acts similarly to the output level LED display found in a stereo receiver/amplifier. Start by connecting six of the Stamp’s pins to one of the LED blocks, each through a resistor. Then write the necessary BASIC code that will display the magnitude of a given number by lighting up a proportional number of LEDs.

Finally, test your code by having the Stamp perform a counting routine as given above in the sample program in Section 1 and display the current value of the counter using the LEDs (e.g. one additional LED lights up for every 50 counts).

 Demonstrate the result to one of the lab assistants.

4. Driving a speaker

The Stamp provides a very convenient means for generating sounds when connected to a speaker using the freqout command. A speaker for this lab has been provided and should be hooked up as in Figure 5. Can you write a little song with this? The Stamp manual has more details on how to generate specific tones. For example, middle C is around 523Hz when using the freqout command. The command “freqout 0,350,523” would play a ‘C’ on pin 0 for 350ms. Beware, the speaker might be a little quiet, so you may need to hold it up to your ear!

Figure 5: Speaker Circuit

 Demonstrate the result to one of the lab assistants.

5. Integrating Everything (Extra Credit)

5 Now that you have all the basics required to construct simple circuits and write code to interface them to the Stamp, try combining the pieces from the first 4 sections of this lab into one more complex setup.

Some ideas to think about: - have the button turn the speaker on and off - change the frequency of the tone played by the speaker using the potentiometer - display that frequency using the LEDs - etc …

Don’t be constrained by the suggestions above. Let your imagination run wild. When you’re satisfied with your setup, demo it to one of the Lab Assistants.

Report

Organize your report into sections, one section for each set of experiments in . Your report should include responses to all questions. Pay special attention to questions against the  symbol. Provide hardcopies of your code.

6