PDA

View Full Version : IR detector



geckogrotto
- 16th August 2006, 17:00
Hi all I'm really new to picbasic and to electronics in general.
So please don't be too harsh on me.

I bought this
http://www.radioshack.com/product/index.jsp?productId=2049723&cp=&origkw=IR+detector&kw=ir+detector&parentPage=search

Here is the back of the package should give more info
http://www.oscarcontrols.com/coinswitch/ledpd.jpg

What I want to do is basically start a timer when the beam is broken and count every time the beam is broken till its broken 100 times then report back the time.

Lets start with the basics.

So far I have 9V in to a 510 Ohm resistor to the IR led then to ground.
Across from that I have 9V to 10K resistor to the collector side and emitter side to ground.
(connecting a led in series I have been able to get the led to light when the beam is broken
What I'm not sure how to do is get a signal off the detector to a 16F628A input so when the beam is broken I can detect it.

Anyone offer any assistance?

BobK
- 16th August 2006, 17:21
Hello Geckogrotto,

Well you're almost there. First you will need to get a 5 volt regulator to drop your 9 volts down to 5 volts for the PIC. Then connect the 10K resistor between 5 volts and the collector of the phototransistor. You can then connect the junction of the 10K resistor and the collector to the input of your PIC. Your program should "see" a low (ground) condition when the "beam" is being detected and a high (5volts) when the beam is broken.

Hope this helps you!

BobK

geckogrotto
- 16th August 2006, 17:27
Thanks for the help Bob. Like I said im really new to electronics even but is there a way to use resistors instead of a voltage regulator?

geckogrotto
- 16th August 2006, 17:43
WOOT Thanks again Bob I dropped the voltage down to like 4.5V using 3AA batteries instead of a 9V, reduced the resistor on the LED and connected like you said... It works perfect! thanks :) I would like to know if there is a way with resistors to do the same thing and use a 9v if possible.

malc-c
- 16th August 2006, 18:01
You could use a 5.1v zenner diode to reduce the 9v down to 5v, but a small 5v regulator should be a better bet.

geckogrotto
- 16th August 2006, 18:21
I guess this is where I get confused with electronics.
I can take a 9V battery and use a resistor to run a 1.5V led without problem but I can't use a resistor the same way to run a 5V chip.

This seems to make sense to everyone but me lol

Ok on to the coding would this chip be able to handle the timer part of this or would I need a dedicated timer chip?

My guess is that there will be about 100 breaks in the beam in a 30 second time.

Currently I'm testing with this code and a led on RB1.
Main:
if PORTB.0 = 1 then
high PORTB.1
else
low PORTB.1
endif


Now that looks like it will work fine but, if the loop happens more than once before the bean is brought back it could count 10 times for every break...

Would this code work and only count once?

broken var byte
broken = 0
Main:
if PORTB.0 = 1 and broken = 0 then
broken = 1
high PORTB.1
else
low PORTB.1
endif
if PORTB.0 = 0 and broken = 1 then broken = 0

goto Main

peterdeco1
- 16th August 2006, 19:01
Hi geckogrotto. Try something like this.
START:
LET BROKEN = 0
LOW PORTB.1

Main:
IF PORTB.0 = 1 then ACTIVATE 'BEAM BROKEN
GOTO MAIN 'BEAM NOT BROKEN

ACTIVATE:
IF PORTB.0 = 1 THEN ACTIVATE 'WAIT FOR BEAM TO RETURN
LET BROKEN = BROKEN + 1
IF BROKEN >= 10 THEN LIGHTUP 'BEAM NEEDS TO BE BROKEN 10 TIMES
GOTO MAIN

LIGHTUP:
HIGH PORTB.1
GOTO LIGHTUP

geckogrotto
- 16th August 2006, 19:12
Thanks for your suggestion.

Can anyone tell me how to turn on a timer then turn it off and get the amount of time that was timed?

Archangel
- 16th August 2006, 20:16
Hi, yes you can reduce the voltage to any set level you choose.
Using Ohm's Law
V. out = V in X (r2 / r1 + r2)

where r1 is the resistor closest to the power in
and r2 is the resistor closest to ground

Example v out = v in or 10 volts X r2, say 500 ohms . . . 5000
divided by r2 + r1, so if r1= 500 ohms you would get 1000 ohms,
so 5000 / 1000 = 5 volts. Simple huh? The problem here is if you lose the ground
foe even an instant then your input sees full supply voltage, also a voltage divider uses power all the time, in this example .01 amp.
OK the attachment didn't work so basicly two resistors in series, one end attached to + and the otherend to minus and you pull power from the connection in the center.

geckogrotto
- 16th August 2006, 20:49
Hi, yes you can reduce the voltage to any set level you choose.
Using Ohm's Law
V. out = V in X (r2 / r1 + r2)

where r1 is the resistor closest to the power in
and r2 is the resistor closest to ground

Example v out = v in or 10 volts X r2, say 500 ohms . . . 5000
divided by r2 + r1, so if r1= 500 ohms you would get 1000 ohms,
so 5000 / 1000 = 5 volts. Simple huh? The problem here is if you lose the ground
foe even an instant then your input sees full supply voltage, also a voltage divider uses power all the time, in this example .01 amp.
OK the attachment didn't work so basicly two resistors in series, one end attached to + and the otherend to minus and you pull power from the connection in the center.

Ok I don't think these actually exist but lets say they do.

If I had exactly 9V in and used
R2 950
R1 760
That would give me exactly 5V out?

About the connection what your saying is resistor to + and the other resistor connected to that and then to the negative and sandwiched in between the 2 would be my output of 5v?

Also if you want shoot me over the attachment to [email protected]

Thanks :)

Archangel
- 16th August 2006, 22:47
That is correct as would 400/500 (notice it adds up to 900) or any variant of above say 1200 /1500 12k/15k what changes as you go upscale is the circuit impeadance (the ability to load the circuit without significant loss of voltage).
If you only want to drive a PIC input pin and not light leds or run motors then use the larger (higher resistance) resistors, that way if you mess up your code and cause an input to become an output, set to low, you will not create a big short.

geckogrotto
- 16th August 2006, 23:14
Thanks again Joe.

Now I just need the most basic code I can find to start / stop the timer and get the results and I will be able to finish the rest.

I searched around the forums but I'm not sure how to implement the code with my chip.

There are internal timers right that you can turn on or off or am I thinking this whole thing wrong and I need to figure out how fast the chip is running then make my own count loop?

mister_e
- 17th August 2006, 04:55
one way in my mind now... Use HPWM on your led emitter, place the IR receiver on RA.4/T0CKI. Use Timer0 to count, Timer1 or Pause to do your sample time. The read the TMR0 register.

Another wich i don't really like but i saw many times... Led emitter directly to Battery via resistor, IR detector to RA.4/T0CKI. Jump in a interupt routine on a TMR0 overflow. Easy neat... may kill the IR Led soon.. battery too.

Do a search for T0CKI, you should find some tips to begin.

sayzer
- 17th August 2006, 08:19
Here is a table again to select a resistor value with respect to current (mA) vs input volt.
Make the rounding as close as possible.



--------


I(mA) R (ohm) R (ohm)
9V 12V

1 9000 12000
2 4500 6000
3 3000 4000
4 2250 3000
5 1800 2400
6 1500 2000
7 1286 1714
8 1125 1500
9 1000 1333
10 900 1200
11 818 1091
12 750 1000
13 692 923
14 643 857
15 600 800
16 563 750
17 529 706
18 500 667
19 474 632
20 450 600
21 429 571
22 409 545
23 391 522
24 375 500
25 360 480
26 346 462
27 333 444
28 321 429
29 310 414
30 300 400
31 290 387
32 281 375
33 273 364
34 265 353
35 257 343
36 250 333
37 243 324
38 237 316
39 231 308
40 225 300
41 220 293
42 214 286
43 209 279
44 205 273
45 200 267
46 196 261
47 191 255
48 188 250
49 184 245
50 180 240
51 176 235
52 173 231
53 170 226
54 167 222
55 164 218
56 161 214
57 158 211
58 155 207
59 153 203
60 150 200
61 148 197
62 145 194
63 143 190
64 141 188
65 138 185
66 136 182
67 134 179
68 132 176
69 130 174
70 129 171
71 127 169
72 125 167
73 123 164
74 122 162
75 120 160
76 118 158
77 117 156
78 115 154
79 114 152
80 113 150
81 111 148
82 110 146
83 108 145
84 107 143
85 106 141
86 105 140
87 103 138
88 102 136
89 101 135
90 100 133
91 99 132
92 98 130
93 97 129
94 96 128
95 95 126
96 94 125
97 93 124
98 92 122
99 91 121
100 90 120
101 89 119
102 88 118
103 87 117
104 87 115
105 86 114
106 85 113
107 84 112
108 83 111
109 83 110
110 82 109
111 81 108
112 80 107
113 80 106
114 79 105
115 78 104
116 78 103
117 77 103
118 76 102
119 76 101
120 75 100
121 74 99
122 74 98
123 73 98
124 73 97
125 72 96
126 71 95
127 71 94
128 70 94
129 70 93
130 69 92
131 69 92
132 68 91
133 68 90
134 67 90
135 67 89
136 66 88
137 66 88
138 65 87
139 65 86
140 64 86
141 64 85
142 63 85
143 63 84
144 63 83
145 62 83
146 62 82
147 61 82
148 61 81
149 60 81
150 60 80


Sayzer August'06
Selecting resistor value with respect to
Current vs input Volt

geckogrotto
- 17th August 2006, 17:04
After some searching I found this
http://www.picbasic.co.uk/forum/showthread.php?t=190

after making some changes I think I will be able to use this to do what im needing.

geckogrotto
- 20th August 2006, 01:17
Due to all the help I have gotten here I have everything worked out. I decided to buy a lcd screen so I could output the results to it but I have no idea how to hook it up.

LCD Display 16 characters with 2 rows LCD its Hitachi HD44780 compatible LSI controller

It has 16 pins.
1 GND Ground
2 +5v Power
3 Vcont Contrast voltage
4 RS Select
5 R/W Read/Write
6 EN Enable
7 DB0 Data bus
8-14 .......
15 LED K Backlight cathode
16 LED A Backlight anode

Ok 1 and 2 are pretty simple :)
Im not sure what pins I would hook up to my 16F628A chip. just data bus?
going to do some searching around to see what I can find was just so excited had to post :) Any help would be appreciated.

geckogrotto
- 20th August 2006, 01:57
Ok looks like mine is almost exactly like the olympic timer
http://www.picbasic.co.uk/forum/attachment.php?attachmentid=84&d=1091819883

So it looks like 7-10 should be data..?

Man I still have so much to learn

mister_e
- 20th August 2006, 09:20
I decided to buy a lcd screen so I could output the results to it but I have no idea how to hook it up.

Free schematic in PBP manual in the LCDOUT section.

geckogrotto
- 20th August 2006, 21:10
Free schematic in PBP manual in the LCDOUT section.


Thanks lol
Good old fashion case of RTFM!

I have one questions about it tho, pin 3 on the lcd connects to the middle of the 20K resistor? I don't understand that part.

also I don't have a 20K resistor should I use 18K , 22K , 18K and 2K in series, or wait till I can get a 20K?

Thanks

mister_e
- 20th August 2006, 22:10
It's a 20K potentiometer or trimmer. It's there to adjust the LCD contrast. You can use lower value. 1-10K still work. You can also use two resistor to make a fix contrast. Wich value... ARRRGGH close to be impossible to tell, no lcd work the same way.

You can still use HPWM to make a software controlled LCD contrast... for now, just plug-it and display on it.
http://www.picbasic.co.uk/forum/showthread.php?t=794&highlight=contrast

geckogrotto
- 21st August 2006, 01:34
Got it :) thanks

geckogrotto
- 21st August 2006, 20:32
Ok last question :)

I have read all over about floating points and I still can't figure out how to do this.

My calculation is

shotspersecond = count / seconds
shotsperminute = shotspersecond * 60

with floating points this would work fine and tell me shots per minute avg.

Without FP if the seconds > count then I get 0 as shotspersecond and a number of other problems.

I know this can't be too hard but I can't seem to wrap my brain around it.


Any help would be very appriciated as once I get this I should be able to finish off my project.

Any help would be very appriciated as once I get this I should be able to finish off my project.

This is what I have so far... It may be accurate enough for what I am doing.
SPS = 5 / TotalTime
if sps = 0 then
SPS = 5 // TotalTime
SPM = sps * 60 / 100
else
SPM = SPS * 60
endif

Better way to do it?

SteveB
- 21st August 2006, 21:04
My calculation is

shotspersecond = count / seconds

a) Declare your variables as WORDS

b) Multiply count by something like 100 or 1000. This shifts the decimal place to the right. Pick a multiplier that will keep the result below 65536.

b) Now, do the divide: shotspersecond = count / seconds.

c) Display shotspersecond with something like:

LCDOUT DEC shotspersecond/100, ".",DEC2 shotspersecond//100

The key is to shift the decimal place prior to the division, then adjust the display to account for the shift.

For more reading (and a better example of integer math), take a look at These threads from Darrel Taylor:
Retrieving 32bit Multiply Result (http://www.picbasic.co.uk/forum/showthread.php?t=24)
Retrieving Div32 Remainder (http://www.picbasic.co.uk/forum/showthread.php?t=48)
32-bit Variables and DIV32, Hourmeter 99999.9 (http://www.picbasic.co.uk/forum/showthread.php?t=1942).

You might also try a search for integer math.

HTH,
Steve

geckogrotto
- 21st August 2006, 21:12
Ohhh thanks Steve I was multiplying x 100 after the divide not before :) thanks I got it now :)

geckogrotto
- 24th August 2006, 20:11
Ok really this time last question. This has more to do with eagle but figured you guys would know.
I'm trying to find a LCD like the one on page 94 in the manual to use with eagle and I can't seem to find one.

I downloaded all the LBR files I could find that said LCD on them but still no luck.

malc-c
- 25th August 2006, 00:27
Mat,

Just select one of the connectors / Pin Headers that suit the number of connections on the LCD. This will allow you to either plug the LCD into your board if you use a suitable pin and header block, or solder wires directly to the pads if the LCD is remote from the board

geckogrotto
- 25th August 2006, 01:05
Thanks malc that will work fine.
For anyone who was interested or helped with the process and wanted to see the end result here is a video.

http://www.gecko-grotto.com/workshop/lcd_counter.wmv

emavil
- 30th August 2006, 01:14
Hi, i'm building a simple remote controlled project consisting of 8 12v Relays. I'm using TSOP1738 IR receiver from Vishay Telefunken. I'm using PIC16F84A microcontroller and PICBasic Compiler. I got it from a junked VCD player. Is there anyone who knows how to intercept the coded signal from any IR transmitter? How do i control individual relays independently.

mister_e
- 30th August 2006, 03:16
look at those links
http://rentron.com/PicBasic1.htm
http://rentron.com/remote.htm
http://www.winpicprog.co.uk/pic_tutorial5.htm
Enjoy and good luck!