background image

Still not quite working?

You may be in a room which is 

either too bright or dark. Try 

turning the lights on or off to 

see if this helps. Or if you have 

a flashlight near by give that a 

try.

It Isn't Responding to 

Changes in Light.

Given that the spacing of the 

wires on the photo-resistor is 

not standad, it is easy to 

misplace it. Double check its in 

the right place

25

CIRC-09

Code 

(no need to type everything in just)

Not Working? 

(3 things to try)

More, More, More:

More details, where to buy more parts, where to ask more questions.

http://tinyurl.com/cpa83c

Making it Better

LED is Remaining Dark

This is a mistake we continue 

to make time and time again, if 

only they could make an LED 

that worked both ways. Pull it 

up and give it a twist.

Reverse the response:

Light controlled servo:

Perhaps you would like the opposite response. Don't 

Lets use our newly found light sensing skills to control a servo 

worry we can easily reverse this response just change.

(and at the same time engage in a little bit of Arduino code 

 analogWrite(ledPin, lightLevel); ----> 

hacking). Wire up a servo connected to pin 9 (like in CIRC-04). 

analogWrite(ledPin, 255 - lightLevel);

Then open the Knob example program (the same one we used 

Upload and watch the response change.

in CIRC-08)

 File > Examples > Library-Servo > Knob

Upload the code to your board and watch as it works 

Night light:

Rather than controlling the brightness of the LED in 

unmodified.

Using the full range of your servo:

response to light, lets instead turn it on or off based on a 

You'll notice that the servo will only operate over a limited 

threshold value. Change the loop() code with.

  void loop(){

portion of its range. This is because with the voltage dividing 

     int threshold = 300;

circuit we use the voltage on analog pin 0 will not range from 

     if(analogRead(lightPin) > threshold){
       digitalWrite(ledPin, HIGH);

0 to 5 volts but instead between two lesser values (these 

     }else{
       digitalWrite(ledPin, LOW);    

values will change based on your setup). To  fix this play with 

     }

the val = map(val, 0, 1023, 0, 179); line. For hints on what to 

}

do visit

 http://arduino.cc/en/Reference/Map .

Download the Code from ( http://tinyurl.com/crdum6 )

(copy the text and paste it into an empty Arduino Sketch)

/*

}

 * A simple programme that will change the 

 /*

 * intensity of an LED based  on the amount of

 * loop() - this function will start after setup 

 * light incident on the photo resistor.

 * finishes and then repeat

 * 

 */

 */

void loop()
{

//PhotoResistor Pin

 int lightLevel = analogRead(lightPin); //Read the

int lightPin = 0; //the analog pin the 

                                        // lightlevel

                  //photoresistor is 

 lightLevel = map(lightLevel, 0, 900, 0, 255); 

                  //connected to

         //adjust the value 0 to 900 to

                  //the photoresistor is not 

  lightLevel = constrain(lightLevel, 0, 255);

                  //calibrated to any units so

      //make sure the value is betwween 0 and 255                                                   

                  //this is simply a raw sensor 

  analogWrite(ledPin, lightLevel);  //write the value
}

value (relative light)
//LED Pin
int ledPin = 9;//the pin the LED is connected to
               //we are controlling brightness so 
               //we use one of the PWM (pulse 
               //width modulation pins)

void setup()
{
  pinMode(ledPin, OUTPUT); //sets the led pin to 
                           //output

24

CIRC-09

What We’re Doing:

.:Light:.

.:Photo Resistors:.

Whilst getting input from a potentiometer can be useful 

for human controlled experiments, what do we use 

when we want an environmentally controlled experiment? 

We use exactly the same principles but instead of a 

potentiometer (twist based resistance) we use a photo resistor (light based 

resistance). The Arduino cannot directly sense resistance (it senses voltage) so we 

set up a voltage divider ( http://tinyurl.com/2sunta ). The exact voltage at the 

sensing pin is calculable, but for our purposes (just sensing relative light) we can 

experiment with the values and see what works for us. A low value will occur when 

the sensor is well lit while a high value will occur when it is in darkness.

The Circuit:

Wire

  

Photo-Resistor
x1

560 Ohm Resistor
Green-Blue-Brown
x1

2 Pin Header
x4

CIRC-09 
Breadboard sheet
x1

Green LED
x1

10k Ohm Resistor
Brown-Black-Orange
x1

Schematic:

Arduino 

pin 13

LED

resistor 

(560ohm)

gnd

(ground) (-)

+5 volts

photo
resistor

resistor 

(10k ohm)

Arduino 

analog

pin 0

Parts:

.:download:.

breadboard layout sheet

http://tinyurl.com/cmzfdu

.:view:.

assembling video

http://tinyurl.com/cdldd6

The Internet

Summary of Contents for ARDX

Page 1: ...Arduino Experimenter s Guide ARDX ARDX arduino experimentation kit ...

Page 2: ...der a Creative Commons By Share Alike license This means you must credit oomlout in your design and share your developments in a similar manner Why We grew up learning and playing with open source software and the experience was good fun we think it would be lovely if a similar experience was possible with physical things more details on the Creative Commons CC By Share Alike License can be found ...

Page 3: ...ircuits CIRC01 Getting Started Blinking LED 08 CIRC02 8 LED Fun Multiple LEDs 10 CIRC03 Spin Motor Spin Transistor and Motor 12 CIRC04 A Single Servo Servos 14 CIRC05 8 More LEDs 74HC595 Shift Register 16 CIRC06 Music Piezo Elements 18 CIRC07 Button Pressing Pushbuttons 20 CIRC08 Twisting Potentiometers 22 CIRC09 Light Photo Resistors 24 CIRC10 Temperature TMP36 Temperature Sensor 26 CIRC11 Larger...

Page 4: ...M assembling the pieces Breadboard x1 Arduino x1 3mm x 10mm bolt x2 3mm nut x4 Arduino Holder x1 c A usin c c p Putting It Together Thi It For an introduction to what an Arduino is visit http tinyurl com 9txjmh ...

Page 5: ...e plug your Arduino board into a free USB port Wait for a box to pop up Step 5 Add new Hardware Skip searching the internet click the next box when prompted to do so Install from a Specific destination click Install from a list or specific location Advanced Choose the Location c program files arduino 00rr drivers FTDI USB Drivers Finished Step 2 Open The dmg Open mount arduino 00 mac dmg rr rr ver...

Page 6: ...etup has finished After it has run once it will be run again and again until power is removed Syntax semicolon Each line of code must be ended with a semicolon a missing semicolon is often the reason for a programme refusing to compile One of the slightly frustrating elements of C is its formating requirements this also makes it very powerful If you remember the following you should be alright mul...

Page 7: ...Used when you would like to repeat a chunk of code a number of times can count up i or down i or use any variable Digital digitalWrite pin value Once a pin is set as an OUTPUT it can be set either HIGH pulled to 5 volts or LOW pulled to ground pinMode pin mode Used to set a pins mode pin is the pin number you would like to address 0 19 analog 0 5 are 14 19 the mode can either be INPUT or OUTPUT in...

Page 8: ...gh it only in one direction Things to watch out for Identifying Will only work in one direction Looks like a mini light bulb Requires a current limiting resistor More Details on Wikipedia http tinyurl com zhpyv P P I P C P Exam gree red R Resistors What it Does No of Leads Restricts the amount of current that can flow 2 through a circuit Things to watch out for Identifying Easy to grab the wrong v...

Page 9: ... read Photo Resistor What it Does No of Leads Produces a variable resistance dependant on 2 the amount of incident light Things to watch out for Identifying Remember it needs to be in a voltage Usually a little disk with a clear top and a divider before it provides a useful input curvy line underneath More Details http tinyurl com c2wdkw Component Details cont Pushbutton What it Does No of Leads C...

Page 10: ...his plug the Arduino board into your USB port Then select the proper port in Tools Serial Port the comm port of your Arduino Next upload the program by going to File Upload to I O Board ctrl U Finally bask in the glory and possibility that controlling lights offers If you are having trouble uploading a full trouble shooting guide can be found here http tinyurl com 89s2po The Circuit Wire 10mm LED ...

Page 11: ...is connected to pin 13 but we can use any of Along with digital on off control the Arduino can control the Arduino s pins To change it take the wire plugged some pins in an analog brightness fashion more details on into pin 13 and move it to a pin of your choice from 0 this in later circuits To play around with it 13 you can also use analog 0 5 analog 0 is 14 Change the LED to pin 9 also change th...

Page 12: ...rduino works Along with controlling the LEDs we start looking into a few simple programming methods to keep your programs small for loops used when you want to run a piece of code several times arrays used to make managing variables easier its a group of variables The Circuit 5mm Green LED x8 560 Ohm Resistor Green Blue Brown x8 2 Pin Header x4 CIRC 02 Breadboard sheet x1 Parts download breadboard...

Page 13: ...herNoLoop Will light one then delay for delayTime then light the next LED it Not Working 3 things to try More More More More details where to buy more parts where to ask more questions http tinyurl com d2hrud Making it Better Some LEDs Fail to Light It is easy to insert an LED backwards Check the LEDs that aren t working and ensure they the right way around Switching to Loops in the loop function ...

Page 14: ...2222AG a rather common general purpose transistor The important factors in our case are that its maximum voltage 40 v and its maximum current 600 milliamp are both high enough for our toy motor full details can be found on its datasheet http tinyurl com o2cm93 note the transistor we use has a in order Base Collector Emitter pinout differing from some other popular transistors The 1N4001 diode is a...

Page 15: ... 50 because our eyes are not quick enough to would like to change the speed of acceleration change the see it flashing on and off The same feature works with variable delayTime larger means a longer acceleration time transistors Don t believe me Try it out Still Not Working Sometimes the Arduino board will disconnect from the computer Try un plugging and then re plugging it into your USB port int ...

Page 16: ...tioning is controlled through a timed pulse between 1 25 milliseconds 0 degrees and 1 75 milliseconds 180 degrees 1 5 milliseconds for 90 degrees Timing varies between manufacturer If the pulse is sent every 25 50 milliseconds the servo will run smoothly One of the great features of the Arduino is it has a software library that allows you to control two servos connected to pin 9 or 10 using a sing...

Page 17: ...rduino s included our favorites library sometimes it is fun to figure out how to program something yourself Try it We re controlling the pulse directly Xmas Hit Counter so you could use this method to control servos on any of the http tinyurl com 37djhq Arduino s 20 available pins you need to highly optimize this code before doing that Open Source Robotic Arm uses a servo controller as well as the...

Page 18: ...Ds and the like using only three arduino pins They can also be linked together to give you a nearly unlimited number of outputs using the same four pins To use it you clock in the data and then latch lock it in latch it To do this you set the data pin to either HIGH or LOW pulse the clock then set the data pin again and pulse the clock repeating until you have shifted out 8 bits of data Then you p...

Page 19: ...r details on how this works try http tinyurl com 6vz53 An Now things get more interesting If you look back to the code from CIRC02 Arduino is very good at manipulating bits and there are an entire set 8 LED Fun you see we change the LEDs using digitalWrite led state this of operators that help us out Details on bitwise maths is the same format as the routine we wrote changeLED led state You can ht...

Page 20: ...element makes a clicking sound each time it is pulsed with current If we pulse it at the right frequency for example 440 times a second to make the note middle A these clicks will run together to produce notes Lets get to experimenting with it and get your Arduino playing Twinkle Twinkle Little Star C N M M P T a t c C o T I t c t a u i M T a g T m w n c d e f g a b C h int int char int int void f...

Page 21: ...eir values in the tones array up or down 1 1 2 4 Happy Birthday first line until they sound right each note is matched by its name int length 13 char notes ccdcfeccdcgf in the names array ie c 1915 int beats 1 1 1 1 1 2 1 1 1 1 1 2 4 Tired of Twinkle Twinkle Little Star The code is written so you can easily add your own songs check out the code below to get started Melody cleft 2005 D Cuartielles ...

Page 22: ... LOW The button is set up to pull the Arduino s pin LOW when it is pressed however when the button is unpressed the voltage of the pin will float causing occasional errors To get the Arduino to reliably read the pin as HIGH when the button is unpressed we add the pull up resistor note the first example program uses only one of the two buttons The Circuit Wire Pushbutton x2 560 Ohm Resistor Green B...

Page 23: ...sy to put it in the wrong way Give it a 90 degree twist and see if it starts working On button off button Fading up and down The initial example may be a little underwhelming ie I Lets use the buttons to control an analog signal To do this you don t really need an Arduino to do this lets make it a will need to change the wire connecting the LED from pin 13 to little more complicated One button wil...

Page 24: ...hen it is connected with 5 volts across its outer pins the middle pin will read some value between 0 and 5 volts dependent on the angle to which it is turned ie 2 5 volts in the middle We can then use the returned values as a variable in our program The Circuit Wire Potentiometer 10k ohm x1 560 Ohm Resistor Green Blue Brown x1 2 Pin Header x4 CIRC 08 Breadboard sheet x1 Parts Green LED x1 Schemati...

Page 25: ...nge the loop code to Sometimes you will want to switch an output when a void loop int value analogRead potPin 4 value exceeds a certain threshold To do this with a analogWrite ledPin value potentiometer change the loop code to Upload the code and watch as your LED fades in relation to void loop int threshold 512 your potentiometer spinning Note the reason we divide the if analogRead potPin thresho...

Page 26: ...tp tinyurl com 2sunta The exact voltage at the sensing pin is calculable but for our purposes just sensing relative light we can experiment with the values and see what works for us A low value will occur when the sensor is well lit while a high value will occur when it is in darkness The Circuit Wire Photo Resistor x1 560 Ohm Resistor Green Blue Brown x1 2 Pin Header x4 CIRC 09 Breadboard sheet x...

Page 27: ...ied Using the full range of your servo response to light lets instead turn it on or off based on a You ll notice that the servo will only operate over a limited threshold value Change the loop code with void loop portion of its range This is because with the voltage dividing int threshold 300 circuit we use the voltage on analog pin 0 will not range from if analogRead lightPin threshold digitalWri...

Page 28: ...we will use some of the Arduino s maths abilities Then to display it we ll use one of the IDE s rather powerful features the debug window We ll output the value over a serial connection to display on the screen Let s get to it One extra note this circuit uses the Arduino IDE s serial monitor To open this first upload the program then click the button which looks like a square with an antennae The ...

Page 29: ... before Serial println temperature Upload the sketch turn on the serial monitor then change the More informative output speed from 9600 baud to 115200 baud in the pull down menu Lets add a message to the serial output to make what is You are now transmitting data 12 times faster appearing in the Serial Monitor more informative To do Temperature Value is Unchanging Try pinching the sensor with your...

Page 30: ...point is fun sometimes you just want to wire up a hundred switches to control something magnificent Relays give you the ability to dream it up then control it with your Arduino Now to using todays technology to control the past The 1N4001 diode is acting as a flyback diode for details on why its there visit http tinyurl com b559mx The Circuit 2 Pin Header x4 CIRC 11 Breadboard sheet x1 Schematic 2...

Page 31: ...Working The included relays are designed to be soldered rather than used in a breadboard As such you may need to press it in to ensure it works and it may pop out occasionally Controlling a Motor In CIRC 03 we controlled a motor using a transistor However if you want to control a larger motor a relay is a good option To do this simply remove the red LED and connect the motor in its place remember ...

Page 32: ... the Creative Commons Attribution Share Alike 3 0 Unported License To view a copy of this licence visit http creativecommons org licenses by sa 3 0 or send a letter to Creative Commons 171 Second Street Suite 300 San Francisco California 94105 USA www oomlout com ...

Reviews: