NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Photoresistor to ADC
May 04, 2011 by Twarter369 |
Hey everyone. I am working on a small project and I am stuck on some of the MCU code. Basically, what I am trying to do is read from a Photoresistor (exact type is unknown)into the ADC (PC5). If the voltage drops past my predetermined Threshold for x milliseconds I want it to toggle an LED (pin PB1). I am stuck on the code to read the ADC and check it against my threshold. Once that is done I can check the state of the pin and act accordingly. |
---|---|
May 04, 2011 by mongo |
You can set it up as one side of a voltage divider and set the threshold on the low input that is usually AGND. It doesn't necessarily have to be power supply ground. |
May 05, 2011 by Ralphxyz |
Hi Twarter369, are you using the Nerdkit tempsensor ADC code? If so you can just monitor adc_read(). Of course the jumpy ADC might bother you. Post your code lets see how you are doing it. Ralph |
May 08, 2011 by Twarter369 |
@ Mongo, I believe I am using it as 1 half of a Voltage Divider. It goes 5v->PR->ADC1 and R1. I was told that this would drop the voltage in relation to the amount of shadow on the PR. Is there a way for me to post the schematics to this thread? @ Ralph I started with the Temp sensor project. I modified the code so that if a certain temp was reached it would flip a pin. However that was the entire program. I also need to morph this into an interrupt so that I can do other things while waiting for this pin to change. Can someone show an example of
I think I am going to hook up my lcd and print some readings to the screen. I would LIKE to be able to convert adc_read to voltage...but I don't even know where to start on that :) P.S. sorry for the wait on my reply I got side tracked with another project! |
May 08, 2011 by Ralphxyz |
Hi Twarter369, essentially adc_read() is representing 0 to 5 volts within 1024 steps. Well the 0 to 5volts is a generalization it is dependent on your actual power supply value. You should be able to figure out the voltage, or a real close approximation. Ralph |
May 08, 2011 by Twarter369 |
OHHHHH that is so helpful. I can use that to test the rest of the system on the LCD. |
May 14, 2011 by Twarter369 |
Okay, so I believe I have hit another wall in this subject. I am working with this code
As you can see, I have set up the ADC. I included the proper ISR(PCInt1_vector) I believe. Now I cant figure out how to tell it to look for PC4 to fire. I should be able to place my finger over the PR and that should activate the ISR() block, right? It obviously isn't or I wouldn't be at a wall! Any help or nudges in the right direction would be greatly appreciated! P.S. some of the commented lines may be a little out of date for the current code. |
May 14, 2011 by hevans (NerdKits Staff) |
Hi Twarter, I think you are mixing up two different concepts here, and I'm not really sure what you are trying to do with your code. You do appear to have set up an interrupt hander for your code, but you I don't see where you enabled the interrupt. You need to toggle a bit on the PCICR register, and then set an appropriate mask a PSMSK register. Check out the interrupts pages of the datasheet pages 70 and 71. This will enable the interrupt, and the interrupt will fire when the pin goes from a logic high to a logic low, or vice versa. This has nothing to do with the ADC, and wont necessarily allow you to measure a precise trigger voltage. Humberto |
May 15, 2011 by Twarter369 |
Humberto, I thought that is what I was doing on lines 44-55...am I mistaken? |
May 15, 2011 by Twarter369 |
I should add that I don't strictly NEED the ADC. The idea is to get a PR to work like a toggle switch using the interrupt handler and a status variable. |
May 15, 2011 by Ralphxyz |
Twarter369. you should look at huzbum's RPM code. You can connect your PR to PB0 (Pin14). I had problems using Pin14 but you could change his code to use most any pin using a different interrupt. I used huzbum's code with a photo transistor in a simulated RPM situation but essentially you just want to capture the pulse of the PR. Ralph |
May 16, 2011 by Twarter369 |
Thank you, I have read the code on that page, it looks likethere are two ISR functions
That specifically deal with the Timer. What _vect would I be using? I haven't found a comprehensive list of the valid _vect choices and their uses. Also, I am trying to avoid using PB0. I don't know enough about the PR and how it functions to know whether or not it would pull that Pin low during startup (thus freezing my chip in Programming mode). Are you suggesting I use PB0 for double duty? Have it connect to the PR and to the switch to ground? I am no EE, but wouldn't that create a short between the PR and ground when you want to be in programming mode? |
May 16, 2011 by Ralphxyz |
Twarter369, look up "pin change interrupt" here in the forums and the Nerdkit's Tutorials and of course the datasheet as Humberto suggested. I would not use PB0 if not needed. Ralph |
May 16, 2011 by hevans (NerdKits Staff) |
Hi Twarter369, I see it, I didn't check the ADC init function for a pin change interrupt setup. As far as the pin change interrupt goes that looks ok. My suggestion is to undo the connection to your photoresistor and try using a wire to touch the pin to gnd then +5, this should trigger your pin change interrupt and you can verify that works as you expected. If it does, then the photoresistor isn't working the way you expect. Humberto |
May 16, 2011 by Twarter369 |
lol, yeah the code was a little disheveled at that moment. I have since added an interrupt_init()method and cleaned up some other minor issues. I will definitely take your advice on testing the code! Another question in the same vein, but different application, is "Can I use my 5.5 v (red) wire from the USB to detect when it is plugged in to/unplugged from a port?" |
Please log in to post a reply.
Did you know that NerdKits make a great parent/child activity? Learn more...
|