Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Trying to read voltage from photoresitor
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Trying to read voltage from photoresitor

by apurcell666 on Thu Dec 19, 2019 1:50 pm

Hello,

Im using an esp32 and am trying to read the voltage from a photoresistor. If I connect to A0 I get the below output

<Serial>
3507
3507
3505
3503
3503
3504
3501
3504
3505
3505
3507
3503
3519
3503
3505
3509
3503
</Serial>

No matter what I do to the photoresistor the values never change or actually match the voltage as read with a multi-meter. If I change the analog pin and update the code it always reads zero from any other pin. Any ideas? What am I doing wrong?

Code: Select all | TOGGLE FULL SIZE
int protocell = 0;
int read_v = 0;
void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
read_v = analogRead(protocell);
Serial.println(read_v);
delay(300);
}

apurcell666
 
Posts: 5
Joined: Wed Jul 04, 2018 12:36 pm

Re: Trying to read voltage from photoresitor

by jps2000 on Thu Dec 19, 2019 2:00 pm

A photoresitor change its resistance with light. It does not output a voltage. So you need another resistor to make a voltage divider.
for example take a resistor ( value 10k ....100k) from A0 to GND and the photoresistor from A0 to VCC (3V3)
Then light lowers the resistor and voltage on A0 gets higher

jps2000
 
Posts: 559
Joined: Fri Jun 02, 2017 4:12 pm

Re: Trying to read voltage from photoresitor

by apurcell666 on Thu Dec 19, 2019 2:18 pm

I know you need voltage divider. That is what I am referring to by trying read voltage. Matter of fact I followed the example given here: https://learn.adafruit.com/photocells/circuitpython I am thinking the board is toast. Used the same circuit and code on a Arduino and it worked fine.

apurcell666
 
Posts: 5
Joined: Wed Jul 04, 2018 12:36 pm

Re: Trying to read voltage from photoresitor

by adafruit_support_mike on Fri Dec 20, 2019 2:27 am

Post a photo showing your hardware and connections and we'll take a look. 800x600 images usually work best.

adafruit_support_mike
 
Posts: 61197
Joined: Thu Feb 11, 2010 2:51 pm

Re: Trying to read voltage from photoresitor

by marktaylr on Tue Jan 14, 2020 11:32 am

Maybe too late for original poster, but in case it helps someone else:

The problem is in the code. Attempting to read A0 by passing a 0 into the Read function. There is always a layer (or two) of redirection between ESP32 internal logical name, pinouts, Arduino logical names, Feather pinouts, ...

From the Feather docs
A0 - this is an analog input A0 and also an analog output DAC2. It can also be used as a GPIO #26. It uses ADC #2
A1 - this is an analog input A1 and also an analog output DAC1. It can also be used as a GPIO #25. It uses ADC #2
A2 - this is an analog input A2 and also GPI #34. Note it is not an output-capable pin! It uses ADC #1
A3 - this is an analog input A3 and also GPI #39. Note it is not an output-capable pin! It uses ADC #1
A4 - this is an analog input A4 and also GPI #36. Note it is not an output-capable pin! It uses ADC #1
A5 - this is an analog input A5 and also GPIO #4. It uses ADC #2

A0 is GPIO 26. Using Arduino code to read the analog value needs 26 as a function argument:

analogRead(26);

marktaylr
 
Posts: 1
Joined: Sat Jan 04, 2020 3:56 pm

Please be positive and constructive with your questions and comments.