loading
NOTE: The diagram is wrong. The sense pin goes on pin 6 (one more to the left), not pin 5. I'll update the diagrams soon.Thanks to brmccollum for the correction. (Diagrams now fixed)

This is an extremely cheap IR proximity sensor you can make with a few cheap parts and an AVR programmer. I use an Arduino as my programmer in this Instructable.

This sensor only has a range of about 3 inches. You can easily add more LEDs or brighter ones to extend the range. You can also easily re-arrange the LEDS to detect when a beam is broken as well.

The design takes advantage of a cheap AVR (computer on a chip). The computer pulses the IR LEDs off and on and compares the analog readings from the sensor in each state. When the reading with the lights on is above the reading with the lights off the sense pin goes high indicating the sensor is seeing it's own (reflected) light. There is an LED on the sense indicator in this design so you can see when the sensor engages. You can connect the signal right to a microcontroller like an Arduino or Picaxe.

This design moves some processing out of your main robot brain and into it's own node. You may want to debounce the signal, but you don't have to flash the leds and take the readings. You can also use just one digital pin to take the reading. The sketch is around 700K out of 1024 available.





Why I built this
I'm way out of high school, but this is part of a series of designs related to bringing the robots from the game Robot Oddysey into the real world. I want to allow grade schoolers the same chance to learn robotics I had. So I am working on building really inexpensive robots that can move in 8 directions without turning. The "bumpers" are complete now.
 
 

Step 1: Gather Materials

The electronics you will need are as follows. Digikey part numers and prices are as of 4/18/2012.

Quantity
Digikey Part Number
Description
Cost
1
475-1439-ND475-1439-ND
PHOTOTRANSISTOR NPN W/FILTER 5MM
$0.53
1
ATTINY13A-PU-ND
IC MCU AVR 1K FLASH 20MHZ 8PDIP
$0.95
3
CF14JT220RCT-ND
RES 220 OHM 1/4W 5% CARBON FILM
$0.24
1
CF14JT1M00CT-ND
RES 1M OHM 1/4W 5% CARBON FILM
$0.08
1
2N3904FS-ND
IC TRANS NPN SS GP 200MA TO-92
$0.18
2
754-1241-ND
EMITTER IR 3MM 940NM WATER CLEAR
$0.44
 
 
TOTAL
$2.42
 

You will also need
  • Electrical tape
  • plastic drinking straw
  • scissors
  • Wire snippers
  • breadboard and/or soldering iron
  • Jumper wires
  • An AVR programmer (any Ardunio compatible will do)
  • Ardunio 1.0 software with modifications to program Attiny and the Atiny13 core 

 

Hi! Thanks for sharing this. I've working on this for a couple of days now since I read this instructable and this article http://www.ikalogic.com/infra-red-proximity-sensor-part-1/ . I'm using a Picaxe 14M chip and a clear capsule phototransistor (w/o filter) So far I found this: The sensor is very sensitive to daylight (I was working next to my room's window LOL ;) ). <br> <br>In order to solve that problem, watching the behavior of the readings on my PC, I programmed my Picaxe to read the value of the phototransistor on the ADC0 (leg 7) with a 8bit resolution and add some to the reading (4 to be specific); the microcontroller takes a first reading of the phototransistor with the IR led (only one), adds 4 to it, takes a second reading of the prototransistor with the led on and compares, if the second is bigger than the first the indicator led goes on. This is reliably working for me between 4&quot; and 1/2&quot; even working next to the window!!! :D <br> <br>Here is the program I downloaded on my Picaxe: <br> <br>main: <br>low 5 <br>readadc 0,b1 <br>debug <br>high 5 <br>readadc 0,b2 <br>debug <br>let b3=b1+4 <br>if b2&gt;b3 then high 4 <br>else low 4 <br>pause 5 <br>endif <br>goto main <br> <br>The IR LED is connected on the output 5 and the indicator LED is connected on the output 4. I'm not familiar with Arduino, so I hope Picaxe people can use this too, this is really helping my little robot so, thanks again!
I am so glad this helped you out. Thanks for the insights. My program could use some tweaking for working in bright lights or sunlight. Right now it can get confused under fluorescent lights that have a similar period.
<p>amazing<br>work mate</p>
<p>I had hoped that your recommendation to include analogReference(0); would solve my analogRead problems on the Attiny13... but sadly it doesnt I updated my core... but no luck.<br>Any suggestions?</p>
<p>seems it is fixed. apparently needed to restart my IDE after the update of the core</p>
<p>I am getting an error: </p><p>###############################################################</p><p>Arduino: 1.5.6-r2 (Windows 7), Board: &quot;ATtiny13 @ 4.8MHz (internal 4.8 MHz clock)&quot;</p><p>Attiny13IRproxsense.ino:22:21: error: Arduino.h: No such file or directory</p><p>Attiny13IRproxsense.ino: In function 'void setup()':</p><p>Attiny13IRproxsense:24: error: 'OUTPUT' was not declared in this scope</p><p>Attiny13IRproxsense:24: error: 'pinMode' was not declared in this scope</p><p>Attiny13IRproxsense:27: error: 'analogReference' was not declared in this scope</p><p>Attiny13IRproxsense.ino: In function 'void loop()':</p><p>Attiny13IRproxsense:34: error: 'PORTB' was not declared in this scope</p><p>Attiny13IRproxsense:36: error: 'analogRead' was not declared in this scope</p><p>Attiny13IRproxsense:42: error: 'millis' was not declared in this scope</p><p>Attiny13IRproxsense.ino: In function 'void delay1()':</p><p>Attiny13IRproxsense:61: error: 'millis' was not declared in this scope</p><p> This report would have more information with</p><p> &quot;Show verbose output during compilation&quot;</p><p> enabled in File &gt; Preferences.</p><p>#################################################################</p><p>I think the problem is that we used the 20 MHz chip you recommend, but there is no 20MHz option in the board menu.</p>
This circuit works for me as described...but according to the datasheet it shouldn't(?) <br> <br>The datasheet for the phototransistor used says that, starting from the long pin side, the pins are: BASE, COLLECTOR, EMITTER (long, long, short). <br> <br>So, actually, the circuit has the BASE connected to GRN through 1M Resistor (and also to the ATTiny input) while the EMITTER goes to +5V. <br> <br>I'm very new to circuits/transistors, but that setup doesn't make any sense to me. Why does it work? Am I misreading the datasheet? (This is the datasheet I'm looking at: http://catalog.osram-os.com/media/_en/Graphics/00045426_0.pdf)
I think that datasheet is wrong. Last time I clicked the link the datasheet was in English and the pins were CBE not BCE. I agree it shouldn't work as specified in that datasheet!
American stuff is usually CBE, while Japanese components are often BCE. But they should have different part numbers to differentiate them. As always, you are the final inspector.
I think it's at least possible that the part you refer to in your instructable (475-1439-ND) refers to a slightly different part on Digi-Spark and the pins are different now. <br> <br>Mine works by ignoring the first long pin and putting the short pin to the 1M resistor (and Pin 3) and the middle pin to +5vcc - so if anyone orders the part from DigiSpark and it's not working the way you think it should - I would try this!
I think you mean digikey. :) <br>You are right! I have the wrong PN! It's 475-1493-ND. Curse my dyslexia!<br>http://www.digikey.com/scripts/DkSearch/dksus.dll?vendor=0&amp;keywords=%09475-1439-ND is the correct link. I'll update the instructable!
If anybody is having trouble programming the ATTiny45/85 bootloader or sketch, I believe there is a bug in the newer version of the Arduino IDE (1.0.2). I mirrored precisely the files and steps I took to do this process successfully and put it in a Google Site for my own records.<br> <br> If it helps anyone else, feel free to check it out <a href="https://sites.google.com/site/arduinosdreamofelectricsheep/programming-attiny85-with-arduino-uno-isp" rel="nofollow">here</a>
Thanks so much for sharing this.
To fix the light and fluorescent interferenc,e use a 38kHz Infrared Receiver Module instead of the phototransistor. <br> <br>Make sure you set the output to the IR led to 38Khz using something like; <br> <br> irsend.enableIROut(38); <br> irsend.mark(0); <br> <br> online they can be had under $,1 at radio shack they are about $4.
You may need this library to set the 38khz part <br> <br>http://www.arcfn.com/2009/08/multi-protocol-infrared-remote-library.html
Keep in mind the Attiny13 has VERY limited space. The posted library may or may not fit.
The novice might be a bit confused in setting up the core files as the MIT page only describes it for the 45-85 core and the 13 core does not come with a boards.txt file, so allow me to expand a bit here: <br>-Go to your sketches folder <br>-most likely there is a folder called 'hardware' if not, create that. <br>-open the hardware folder <br>-create a folder called 'attiny13' <br>open that folder <br>-create a new folder called 'core13' <br>-open that folder and unzip the attiny core files as found in the link in the beginning of this article <br>-go back to your /hardware/attiny13 folder <br>-create a text file called 'boards.txt' <br>open that file. <br>paste the following text in that file: <br>########################################################################### <br> <br>attiny13.name=Attiny13 @ 128 KHz (internal watchdog oscillator) <br> <br>attiny13.upload.using=arduino:arduinoisp <br># attiny13.upload.protocol=avrispv2 <br># attiny2313at1.upload.using=pololu <br> <br>attiny13.upload.maximum_size=1024 <br>attiny13.upload.speed=250 # important for not losing connection to a slow processor <br> <br>attiny13.bootloader.low_fuses=0x7B <br>attiny13.bootloader.high_fuses=0xFF <br> <br>attiny13.bootloader.unlock_bits=0x3F <br>attiny13.bootloader.lock_bits=0x3F <br> <br>attiny13.build.mcu=attiny13 <br>attiny13.build.f_cpu=128000 <br>attiny13.build.core=core13 <br> <br>############################################################### <br> <br>attiny13at4.name=ATtiny13 @ 4.8MHz (internal 4.8 MHz clock) <br>attiny13at4.bootloader.low_fuses=0x69 <br>attiny13at4.bootloader.high_fuses=0xff <br>attiny13at4.upload.maximum_size=1024 <br>attiny13at4.build.mcu=attiny13 <br>attiny13at4.build.f_cpu=600000 <br>attiny13at4.build.core=core13 <br>############################################################### <br> <br>attiny13.name=ATtiny13 @ 9.6MHz (internal 9.6 MHz clock) <br>attiny13.bootloader.low_fuses=0x7a <br>attiny13.bootloader.high_fuses=0xff <br>attiny13.upload.maximum_size=1024 <br>attiny13.build.mcu=attiny13 <br>attiny13.build.f_cpu=9600000L <br>attiny13.build.core=arduino:arduino <br>attiny13.build.variant=tiny8 <br>########################################################################### <br> <br>save it. <br>restart your Arduino IDE (if you had it open, close it and then start it again) <br>check if you can see the newly added Attiny13 board under 'tools-boards' <br>
Nice! You should make this into an Instructable and I will link to it. Also you need to edit one of the core Arduino files to fix an issue with the timer. It's on one of the pages I linked.
I am the maintainer of the Core13 project. <br>Can you please describe the problem that you experienced with the timer and I will look for a solution and try to incorporate it permanently. <br>
hardware/arduino/cores/arduino/wiring.c: 44 <br> <br>-#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) <br>+#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny13__) <br>SIGNAL(TIM0_OVF_vect) <br> <br>This page describes changing wiring.c at the end of the post. <br> <br>http://tekstop.wordpress.com/2011/12/30/programming-attiny13-using-arduino-isp-and-all-the-hacking-involved/ <br>
That is odd. <br>No modifications should be necessary. I will check it out.
Perhaps the mod is not needed? I haven't double checked but I seem to recall the blink sketch not working just right until I did this.
P.S. I love Core13! Thanks so much for doing it.
Well, apparently I made a mistake in the above description, that I will correct. It needs to be nested one layer deeper. yeah i saw the mistake in the boards.text. <br>Anyway, I have it working now so I will write up something in a seperate ibble
explained here. <br>http://www.instructables.com/id/Installing-Attiny13-core-files/ <br>the boards.txt file from the website had a couple of other peculiarities, but this shld make it all clear
Thanks again. Linked in the main body of the Instructable.
Would this work with an attiny85v?
Can't see a reason why not. I think the 13, the 45 and the 85 are completely interchangeable, but just ahve a difference in memory space. <br> <br>I just can't get over how dirt cheap the Attiny is at Digikey. I pay 3 times that price over here in Western Europe. Might be worth going to digikey if my order is big enough to offset the shipping cost
Shouldn't the red led be on the 6pin of the attiny13 not the 8pin? I just breadboarded it and it only works on the 6pin.
Good catch! I'll look for the typo now. Otherwise how is the sensor working for you?
Its working well, I'm going to try a couple of different resisters and 3d printed enclosures to see is I can get the sensitivity I want, thanks for the design.
That's great news. It's always nice to hear someone actually made or did your Instructable and it worked. I think there is room for improvement in the program as well, but I haven't had time to experiment. Thanks for the feedback. If you get a good enclosure please consider putting it on thingaverse.
&gt; compare += analogRead(trimPin); <br> <br>What/where is trimPin? Did you have a potentiometer connected to another pin to tune the behavior at one point? <br>
Sorry for uploading slightly out of date code. The trimmer proved superfluous. I'll fix the code soon. Should work as is.
The code is up to date now. Good catch plocher.
Cool.

About This Instructable

32,810views

113favorites

License:

Bio: "If you want to build a ship, don't drum up people to collect wood and don't assign them tasks and work, but rather ... More »
More by dustinandrews:Quick Coffee Grinder Hummus Make a desktop trebuchet Easy solar furnance with Grid Beam 
Add instructable to: