MACROS class: Some notes ------------------------ These notes are to help you understand your experiments a bit more. They are also for your safety. For example, if you short a battery, it can get very hot and you can get a somewhat nasty surprise (it helps reinforce learning, though :-). It's also sad to see your favorite LEDs burn out and go dead... I recently had the obituary of my favorite big green LED... There is a PDF document containing the figures mentioned below. Read the text below and also look at the figures. Figure 1: ========= This figure shows how batteries are connected to devices (like motors). Batteries provide electrical energy for our experiments. You were given the following kinds of batteries: * 9 Volts (new) * 9 Volts (old) * Two `back to back' 1.5 volt batteries (adding up to 3 Volts) Figure 1 shows a circuit in which a battery is connected to a DC motor. A DC motor is what you got in your first lab. A battery has a positive (+) and a negative (-) terminal. The battery is drawn in the form of a long line parallel to a short line. The (+) terminal is the long line, and the (-) is the short line. Current flows from a battery out of the (+) terminal, through the ``load'' (in our example, the motor). It then flows back into the battery's (-) terminal. Think of ``current'' as positively charged particles. Imagine the positively charged particles walking out of the (+) terminal, walking through the wires, the load, and getting back into the (-) terminal. From the (-) terminal, they walk inside the battery and re-emerge at the (+) terminal. Current can flow only along such a closed path called a ``circuit.'' If instead of a motor, you connect LEDs to a pair of new batteries, you will invariably destroy it. It will glow VERY BRIGHT for a few seconds, and then burn out entirely! More on preventing such things soon. Basically you will need a resistor. Figure 2: ========= Voltage is similar to ``pressure'' Current is similar to ``amount of water flowing'' Resistance is the ``opposition to flow'' To illustrate these ideas, do the experiment shown in Figure 2. Here, a tall can is pierced at three points with identical holes, and filled with water. The bottom-most hole experiences the highest pressure (voltage). Therefore, it shoots the water most horizontally and fastest (this is similar to ``lots of current''). The top-most hole shoots the water the slowest because it has the lowest pressure (er, lowest voltage - by way of analogy). So, for the same resistance (i.e., hole size), the amount of water (i.e., current) increases with increase in voltage. So , if you fix the resistance, then current is proportional to voltage (if you increase the voltage, you increase the current). Figure 2 shows how to depict resistances, currents, and voltages in a circuit. Basically I show a battery connected to a resistor. I provide the equation for calculating the current (I), given the resistance (R), and voltage (V). The equation is I = V / R , and this is known as Ohm's law. Figure 3: ========= What happens if we simply change the resistance, keeping the voltage the same? Let us do an experiment -- or think about the experiment, as it will be all too obvious (in other words, you can do a "thought experiment"). Fill two cans with water, and poke a small hole in one, and a larger hole in the other. The large hole is like lower resistance - hence we get more current. Figure 4: ========= Old batteries versus new: * Old batteries offer a lower voltage * But they are "old" mainly because they offer a higher internal resistance * That is, when the positive charge tries to journey within the battery, going from the negative to the positive, the battery material impedes the progress more in an old battery. * Therefore, if you short an old battery, nothing bad happens * If you short a new battery, it really heats up - watch out! Figure 5: ========= So how do we protect LEDs and make them shine without burning them up? Connect a series resistor. How to find out its value? See the figure! Also see http://home.cogeco.ca/~rpaisley4/LEDcalc.html which provides a calculator on the webpage. Suppose we have a new 9V battery. Suppose we are using a red LED. A red LED takes away 2 Volts We have only 9 - 7 = 2 Volts left. We can pass only 15 milli Amperes before an LED gets too hot 15 milli Amperes , or 15 mA is 0.015 Amperes Therefore R needed = 7 / 0.015 = about 466 Ohms Figure 6: ========= Using a multimeter, you can measure * Voltages (you know how) - Put in range that best accommodates the voltage - Observe polarities and measure voltage * Currents (you can put in series with circuit as shown below) * Resistance - Put in range that best accommodates the resistance - Zero the needle - Then connect to the item whose resistance is being measured and read the value of the resistance appropriately - Let us try to find out your body resistance from one hand to the other! . wet finger-tips (guess how?!) . pinch one multimeter probe with one hand's index finger / thumb . ditto for the other hand . How "conductive" are you ?!?! . How conductive is a potato ? . Are you more conductive than a potato ? :-) Figure 7: ========= General tips on bread-boarding. Figure 7 illustrates how to connect an LED and a resistor in series to a battery. Figure 8: ========= The "microchip" we used (it is called an IC or Integrated Circuit). Its part number is DS 2003. DS 2003 can take a "weak" signal on its input, and deliver a powerful signal on its output. I have shown how a weak current flow from the PC-side is amplified to become a strong current flow through the LED and resistor. Figure 9: ========= Connections for the music-maker program are in this figure. With this circuit, you can run the program music1.bas, and play do-re-mi. You should cup the speaker in your hand (to simulate a baffle) and see how the sound builds up! The webpage http://buzz.ifas.ufl.edu/585a.htm shows a picture of Oecanthus fultoni (snowy tree cricket) chirping away, sitting in a hole in a leaf, with the leaf serving as a sound baffle! Figure 10: ========== Producing binary codes from decimal values can be studied using this circuit. Output different constants and see various combinations of LEDs light up. For instance, outputting these numbers, you will see these LEDs light up: Output number LEDs that light up (0 means OFF, 1 means ON) ---------------------------------------------------------------------- 0 0 0 0 0 1 0 0 0 1 2 0 0 1 0 3 0 0 1 1 4 0 1 0 0 5 0 1 0 1 6 0 1 1 0 7 0 1 1 1 8 1 0 0 0 9 1 0 0 1 10 1 0 1 0 11 1 0 1 1 12 1 1 0 0 13 1 1 0 1 14 1 1 1 0 15 1 1 1 1 Figure 11: ========== Stepper motor connections. * We can connect as shown in the figure. * Notice: It is important to connect pin 9 to power supply, as we are dealing with a motor now. * The connections between pins 16, 15, 14, and 13 of DS 2003, as well as the stepper motor wires Red, White, Orange, and Blue can be arbitrary * We can assign values to C1, C2, C3, and C4 in all possible ways till one assignment works (see music1.bas for these constants) Figure 12: ========== Other experiments possible: * Connect DC motor to LED; spin motor shaft by hand; you can see LED glow. This shows how one can use wind turbines or water turbines to generate electrical power * Surface tension boat: Put styrofoam with one edge coated with soap in a bathtub full of water. The piece will race (reduced surface-tension in water) * Solar boat: Connect solar panel to cooling fan motor (to be given to you). Stand in bright sunlight. Does the fan spin fast? If so, you can build a solar boat as follows - hot-melt glue fan to styrofoam (I can do this for you) - put solar panel on styrofoam as if it is a raft. Connect solar panel to cooling fan Figure 13: ========== We will go through the BASIC programs one by one, and understand programming. The programs are listed below one after the other (they were handed to you during the lab of June 10th). Qbasic comments start with a single quote-mark. ' P1.BAS ' ====== ' ' This program illustrated singe-stepping and turning LEDs on/off ' OUT &H378, 0 ' LED-0 off after this instruction OUT &H378, 1 ' LED-0 on after this instruction OUT &H378, 0 ' LED-0 off after this instruction OUT &H378, 1 ' LED-0 on after this instruction ' ' End of P1.BAS ' P2.BAS ' ====== ' This illustrates how different lights can be turned on/off ' Change the numbers and turn off/on other LEDs too OUT &H378, 2 OUT &H378, 1 OUT &H378, 2 OUT &H378, 1 ' P3.BAS ' ====== ' This illustrates the principle of looping ' Looping (using a "FOR LOOP") allows you to ' repeat tasks multiple times - in this case ' the LED on/off happens 100,000 times. ' You will see the LED half-lit, as it is ' flickering so fast. ' FOR i = 1 TO 100000 OUT &H378, 2 OUT &H378, 1 OUT &H378, 2 OUT &H378, 1 NEXT i ' P4.BAS ' ====== ' This shows how to slow things ' down; you can now connect a speaker ' and listen to the "ptt..ptt..ptt.." sound ' ' Note also the use of subroutines to "help" ' the main program. ' ' DECLARE SUB delay (dur!) dur = 2000 FOR i = 1 TO 100 OUT &H378, 2 CALL delay(dur) OUT &H378, 1 CALL delay(dur) OUT &H378, 2 CALL delay(dur) OUT &H378, 1 CALL delay(dur) NEXT i SUB delay (dur) FOR i = 1 TO dur: NEXT i END SUB ' P5.BAS ' ====== ' If you change "dur" to other numbers (say 100) ' as below, the speaker produces an audio tone. ' Then you can experiment with the hand speaker ' baffle experiment too, like an Oecanthus does. ' DECLARE SUB delay (dur!) dur = 100 FOR i = 1 TO 1000 OUT &H378, 2 CALL delay(dur) OUT &H378, 1 CALL delay(dur) OUT &H378, 2 CALL delay(dur) OUT &H378, 1 CALL delay(dur) NEXT i SUB delay (dur) FOR i = 1 TO dur: NEXT i END SUB ' MUSIC1.BAS ' ========== ' This plays the do-re-mi song ' DECLARE SUB note (dur!, bigdur!) DECLARE SUB portout (code!) DECLARE SUB delay (duration!) CALL note(75, 200) CALL note(65, 100) CALL note(58, 200) CALL note(75, 100) CALL note(58, 200) CALL note(75, 200) CALL note(58, 300) CALL note(65, 300) CALL note(58, 200) CALL note(55, 300) CALL note(55, 300) CALL note(65, 300) CALL note(55, 300) SUB delay (duration) FOR x = 1 TO duration: NEXT x END SUB SUB note (dur, bigdur) FOR z = 1 TO bigdur portout (1) delay (dur) portout (0) delay (dur) NEXT z END SUB SUB portout (code) OUT &H378, code END SUB ' STEPTEST.BAS ' ============ ' This tests the stepper motor out. ' Connect the colors white, red, orange, and blue ' at random. Then play with different sequences ' for 1, 8, 4, 2 -- i.e. try all these sequences: ' ' 1,2,4,8 ' 1,2,8,4 ' 1,4,2,8 ' 1,4,8,2 ' 1,8,2,4 ' 1,8,4,2 ' ' ..and one of these sequences will cause rotation ' (all others cause just wobbles) ' FOR z = 1 TO 8 out &h378, 1 out &h378, 8 out &h378, 4 out &h378, 2 NEXT z PRINT "done!" END SUB delay (duration) FOR x = 1 TO duration: NEXT x END SUB SUB portout (code) PRINT code OUT &H378, code END SUB ' STEPRL.BAS ' ========== ' Finally, the full "animation" DECLARE SUB once (c1!, c2!, c3!, c4!, steps!, duration!) ' Declare DECLARE SUB portout (code!) ' some DECLARE SUB delay (duration!) ' subroutines full = 8 ' A full rotation of the shaft takes 8 iterations inside subroutine ' "once" half = 4 ' A half rotation dur = 2000 ' This constant helps space the application of the stepper motor ' pulses with the correct duration bigdur = 20000 ' This constant decides how much delay to put in after four calls ' to subroutine "once" c1 = 1 ' output "1" to pull motor shaft to position "1" c2 = 4 ' the next position is "4" and so output this constant c3 = 2 ' the next position is "2" and so output this constant c4 = 8 ' the last position is "8" and so output this constant FOR z = 1 TO 8 ' This loop can be changed to arrange a different "dance" CALL once(c1, c2, c3, c4, full, dur) ' This turns clockwise a full rotation CALL once(c4, c3, c2, c1, half, dur) ' This turns counterclockwise half a rotation CALL once(c1, c2, c3, c4, full, dur) ' This turns clockwise a full rotation CALL once(c4, c3, c2, c1, full, dur) ' This turns counterclockwise a full rutation delay (bigdur) ' Delay for some time before the next iteration NEXT z ' Change suggestions: Please change "dur", "bigdur", and anything inside ' the "for z = 1.." loop above. PRINT "done!" END SUB delay (duration) ' This is how you introduce delays FOR x = 1 TO duration: NEXT x END SUB SUB once (c1, c2, c3, c4, steps, duration) ' This conducts the four steps FOR y = 1 TO steps ' to move the motor portout (c1) delay (duration) portout (c2) delay (duration) portout (c3) delay (duration) portout (c4) delay (duration) NEXT y END SUB SUB portout (code) ' This performs the actual output to the motor PRINT code OUT &H378, code ' 378 is the address of the PC parallel port (in Hex) END SUB ' for most PCs. For one PC, I also saw "3BC" as its address. -- end of notes.txt