Objectives: Learn how to input and output analogue values

advertisement
Objectives:
 Learn how to input and output analogue values
 Be able to see what the Arduino is ‘thinking’ by sending
numbers to the screen
By the end of this session:
 You will know how to write a program to send values to
the computer screen and also dim LEDs.
© S. Pithouse for use by Warwick Technology Volunteers
Page: 1
Arduino Uno
Breadboard
Jumper Wires
Potentiometer
LEDs
Resistors
Potentiometers are used in objects such as volume
controls and radios, they are resistors that change
their value depending on their position. The Arduino
can read this value as a number between 0 and
1024.
LDR’s (Light Dependent Resistors) change their
resistance when light changes, this can be read by
the Arduino and used in programs.
LDR
© S. Pithouse for use by Warwick Technology Volunteers
Page: 2
1)
Insert the potentiometer into the
breadboard and connect three
wires as shown in the diagram.
2)
Connect a wire and resistor as
shown in the diagram.
3)
Finally, connect an LED and a
wire between the Arduino’s
pin 9 and the LED.
© S. Pithouse for use by Warwick Technology Volunteers
Page: 3
1)
Plug the USB cable into the Arduino and Computer, then open the Arduino
Program.
2)
Visit the webpage where you found this document —and copy and paste the
‘Analog template program’ into the Arduino window.
3)
The program sets ‘LED’
(pin 9) as an OUTPUT in the
setup function.
It then enters the loop and
saves the value read from
the potentiometer into a
variable called value. This is
between 0—1023.
The final part sends the
value to the LED. It needs to be divided by 4, since the output range is
between 0—256.
© S. Pithouse for use by Warwick Technology Volunteers
Page: 4
4)
Upload the program by clicking on the upload button.
5)
Your LED should change in brightness when you turn the potentiometer. It
would be useful to know what value the Arduino is using, so we are going
to make the Arduino ‘talk’ to the computer to show us the value.
Add the following line of code to the bottom of the setup function:
This starts the conversation between the Arduino and the computer. The
number is the speed at which it talks.
6)
Then add the following lines of code in the loop function:
This sends the potentiometer value to the computer screen.
© S. Pithouse for use by Warwick Technology Volunteers
Page: 5
7)
Upload the program by clicking on the upload button.
8)
Open the Serial Monitor by clicking on the Serial Monitor button.
9)
Check that the number
displayed in the bottom right
corner is ‘115200’, if not, click
on the drop down menu and
choose it.
9)
You should see numbers appear in the window, turn the potentiometer and
watch the values change. These numbers are being used to set the
brightness of the LED.
© S. Pithouse for use by Warwick Technology Volunteers
Page: 6
10)
We can also use the number from the potentiometer in a different way.
Such as changing the delay of a flashing LED.
Replace your loop function with the
code shown on the right.
11)
Upload the program and open the Serial Monitor.
12)
You should see that when the numbers get smaller, the LED flashes faster.
However, when it flashes really quickly, it becomes too quick for us to see,
instead it just looks dimmer. This is how the ‘analogWrite’ function works.
13)
Other devices can be used to input an analogue value, not just a
potentiometer. For example we can replace the potentiometer with an LDR.
© S. Pithouse for use by Warwick Technology Volunteers
Page: 7
14)
Remove the potentiometer and plug in the LDR and resistor as shown in
the diagram below:
Place your hand over the LDR, and you should see that the LED flashes at
a different rate.
Extension task
If you finish all of these steps, try making the LED flash twice as slow. Also
get the Arduino to send a message to the computer every time it turns the
LED on (Hint, you will need this line of code:)
© S. Pithouse for use by Warwick Technology Volunteers
Page: 8
Download