Engineering and Computational Thinking For Kids

Robotics with Arduino Engineering and Computational Thinking For Kids Appendix A - The Voltage Divider ................................................
5 downloads 0 Views 2MB Size
Robotics with Arduino

Engineering and Computational Thinking For Kids

Appendix A - The Voltage Divider ...........................................................................................................2 Appendix B – Arduino Hardware diagram ...............................................................................................4 Appendix C – digitalRead/Write and analogRead/Write..........................................................................5 Appendix D – Pulse Width Modulation (PWM ) .......................................................................................6 Appendix E – Programming the PING Sensor ........................................................................................7 Appendix F – Fritzing Diagrams ..............................................................................................................8 Appendix G – Activities .........................................................................................................................10 EX.1 – CIRCUIT DIAGRAM EXERCISES 10 EX.2 FILL THIS SECTION: 11 EX. 3 BASICS PROGRAMMING EXERCISES 12 Ex. 4 – Voltage Math......................................................................................................................14 Appendix H – Bill of Materials ..............................................................................................................15

3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876

(908)595-1010

www.stormingrobots.com P. 1 | Page

Robotics with Arduino

Engineering and Computational Thinking For Kids

The circuit diagram to the left represents a simple voltage divider. Voltage dividers have two resistors connected in series with a connection in between the two resistors. In this example, resistors labeled R1 and R2 are 220Ω. R1 is closer to the positive voltage of our power source. Vin is the voltage of our power source and Vout is the voltage output from the voltage divider. Given this circuit, we can determine Vout relative to Vin.

The formula is:

So if Vin = 5v and R1 = 220Ω and R2 = 220Ω, then

If we replaced the R1 with a larger resistor, say 330Ω, then our equation would be:

R1 (or even R2) changes.

As you can see, as R1 becomes relatively bigger than R2, Vout gets smaller. That is to say the voltage at Vout changes as the resistance of

Now… why should you care about this at all? Simple… many of the analog sensors that exists are simply variable resistors. Their resistance increase or decrease based on changes in its environment. For example, a thermistor is a resistor whose resistance changes based on temperature. A photoresistor’s resistance decreases when the light on its surface increases. A force sensitive resistor’s resistance decreases when more force is applied to its surface. Like most microcontrollers, the Arduino cannot measure resistance but is quite good at measuring voltage. By putting these analog sensors in a voltage divider circuit, we can measure the change in resistance because our V out voltage will change.

3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876

(908)595-1010

www.stormingrobots.com P. 2 | Page

Robotics with Arduino

Engineering and Computational Thinking For Kids

Ok…. How do I hook it up?? Glad you asked let us proceed to the next page.

Hook up the above circuit and run the program that displays the analog value of A0 in the serial monitor. As discussed in class, the analog pins (A0-A5) can detect the voltage in a circuit. However, the photoresistor doesn’t modify voltage but it modifies the current by changing its resistance. Let’s follow the circuit above starting from the 5v line. 5v connects to the photoresistor (R1 in our formula), which is then connected to a 220Ω resistor. The resistor is then connected to ground completing the circuit. Also note though that a wire connects the A0 pin to where the photoresistor and resistor are connected. The voltage divider and its formula are simple but important to remember.

3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876

(908)595-1010

www.stormingrobots.com P. 3 | Page

Robotics with Arduino

Reset button

Engineering and Computational Thinking For Kids

The pins numbered 0 – 13 on this side are the digital I/O pins.

Digital pins with a tilde ~ are also used for Analog Output using the analogWrite() function

TX/RX 1/0 are for serial I/O and should not be used except for hooking up a serial device such as an Xbee or a GPS.

3v & 5v Power pins and 2 ground pins Vin is voltage in pin to power the Arduino from a shield.

3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876

The pins numbered A0 to A5 are Analog pins used for analogRead 0 to 1023 A0-A5: analogRead() only

(908)595-1010

www.stormingrobots.com P. 4 | Page

Robotics with Arduino

Engineering and Computational Thinking For Kids

Function

Description

digitalRead(pin)

Returns either HIGH or LOW for the pin

digitalWrite(pin,val)

analogRead(pin)

analogWrite(pin,val)

Sets the pin to either HIGH or LOW

Reads in the voltage detected on the pin. Return value from 0.. 1023 Creates a PWM signal on the pin. Val is a value between 0…255

3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876

Pins it can be used on All digital pins 0-13

Example pinMode (9,INPUT); pinMode (14,INPUT);

All analog pins BUT are labeled 14-19 where A0 = 14 and A1 = 15 etc…

if (digitalRead(9) == HIGH{ … }

All digital pins 0-13

val = digitalRead(14); //Pin A0 pinMode(9,OUTPUT);

All analog pins BUT are labeled 14-19 where A0 = 14 and A1 = 15 etc… Only analog pins A0 – A5

digitalWrite(9,HIGH); delay(1000); digitalWrite(9,LOW); if (analogRead(A0) > 512){ }

The tilde (~) pins only pins 3,5,6,9,10,11

(908)595-1010

analogWrite(3,127); //half analogWrite(9,255);

www.stormingrobots.com P. 5 | Page

Robotics with Arduino

Engineering and Computational Thinking For Kids

Things to Remember:



analogWrite(pin,value) – pin is the pin to write out to, value is a number between 0 – 255. 0 sends out 0 volts and 255 sends out the max voltage of 5 volts. A number in between would send out a fraction of the voltage such as voltage. 0% Duty Cycle – analogyWrite(0)

25% Duty Cycle – analogWrite(63) ; // out of 255

50% Duty Cycle – analogWrite(127) ;

75% Duty Cycle – analogyWrite(192)

100% Duty Cycle – analogyWrite(255)

As you recall we used the analogWrite() function to send a variable voltage to both dim an LED and to control the amount of power going to our motor. But that isn't exactly what is happening. Example: I want to dim an LED to half its maximum brightness - analogWrite(pin,127) The Arduino will then send a series of timed pulses where the HIGH signal is sent for half the time (50%) and a LOW signal is send for the other half of the time (50%). By doing this, the Arduino creates an "average voltage" on the wire of 2.5 volts. Half the time the LED is powered at 5 volts and the other half it is turned off. Ok this might be neat but why is this important to know? AnalogWrite is used (indirectly) to power motors. Motors are physical devices that may or may not “respond well” with the pulsing of voltages. In our class the motors we use will probably not have this problem for they are small and the load on them is negligible. Also our motor shield will utilize capacitors to “smooth out” the pulses (this is what capacitors do). However we hope you continue to build robots going forward and understanding PWM can help troubleshoot some non-obvious problems.

3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876

(908)595-1010

www.stormingrobots.com P. 6 | Page

Robotics with Arduino

Engineering and Computational Thinking For Kids

1

1

We make signal line go LOW for _________ microseconds.

2

Set Signal Line HIGH for _______ microseconds

3

Here we set signal line LOW. This activates the PING sensor and it sends a sonic pulse.

4

PING sensor will set the signal line HIGH for the amount of time it took for the 'ping' to make its round trip. This is the duration what wemeasure and convert to distance

3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876

(908)595-1010

www.stormingrobots.com P. 7 | Page

Robotics with Arduino

Engineering and Computational Thinking For Kids

These are diagrams for wiring up your Arduino to circuits on the breadboard. They are to be used to complete some of the exercises. The software used to make these diagrams is called Fritzing and it is a free download from http://fritzing.org. Many books and online tutorials use this to display how to wire up particular devices to an Arduino and it is very useful for documenting your own designs.

E1-LED to pin 9

E2 - Potentiometer Comments: In this example we hooked up the potentiometer to the A0 pin but any ‘analog in’ pin (A0-A5) will work. You just have to make sure your code reflects the pin you choose to use.

3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876

(908)595-1010

www.stormingrobots.com P. 8 | Page

Robotics with Arduino

Engineering and Computational Thinking For Kids

E3 - RGB LED Comments: The second pin hooked up to the black ground wire is the long pin on the RGB LED.

E4 – Pullup and Pulldown Resistors

3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876

(908)595-1010

www.stormingrobots.com P. 9 | Page

Robotics with Arduino

Engineering and Computational Thinking For Kids

Ex.1 – Circuit diagram Exercises Complete the following circuit exercises (use UNO as your power source for now) 1. Light up LED 2. Potentiometer with led. a. LED OFF when Potentiometer stays low. LED ON when Potentiometer stays high. b. LED ON when Potentiometer stays low. LED OFF when Potentiometer stays high. 3. Push button with led a. LED OFF when switch is open. LED ON when switch is closed. b. LED ON when switch is open. LED OFF when switch is closed. 4. Photoresistor with led a. LED OFF when the photoresistor detects brightness. LED ON when photoresistor detects darkness. b. LED ON when photoresistor detects brightness. LED OFF when photoresistor detects darkness. 1.

3322 Rt. 22 West, Suite 402, Branchburg, NJ 08876

(908)595-1010

www.stormingrobots.com P. 10 | Page

Robotics with Arduino

Engineering and Computational Thinking For Kids

Ex.2 Fill this section: Pins Digital

Read void setup( ) { pinMode( _____, __________ ); … } void setup( ) { ….. digitalRead( _____ ); … }

Analo g

The analog pins are ____________________________ ___

X = analogRead( A3 );

Write pinMode(_____,________ __); Enable an internal 20K pullup resistor: digitalWrite(_____, __________); Enable an internal 20K pulldown resistor: digitalWrite(_____, __________);

analogWrite(A3, val ); write the min and max for “val”: ________

Suggest Documents