I want to read The dust density with The Sharp GP2Y10. The Sensor ist connected to ESP32. I implemented the Module with Arduino Sketch and it Workstation eine. Now I migrated the Routine to Micropython but I get only zero as the result. Please Help.
Hi Muenny.
I’ve never used that sensor. But after searching for a while I see that it is an analog sensor.
So, to get the sensor value you need to read an analog pin.
What pin are you using and what method in MicroPython are you using to read the sensor?
Regards,
Sara
Hi Sara,
this is the code I use to read the sensor:
#DUst Sensor Sharp GP2Y1010AU0F
######################
# 1 blau LED R 150 Ohm
# 2 gr眉n LED Ground
# 3 wei脽 LED Pin 16
# 4 gelb Ground
# 5 schwarz V0 measure Pin 32
# 6 rot Vcc 3,3V
######################
from machine import Pin, ADC
from utime import sleep, sleep_us
measurePIN = ADC(Pin(32))
#measurePIN = Pin(32)
measurePIN.atten(ADC.ATTN_11DB) #range 0-4095 -> 3,3 V
LedPower = Pin(16)
samplingTime = 280 #original 280
deltaTime = 40
sleepTime = 9680
voMeasured = 0
calcVoltage = 0
dustDensity = 0
print(‘***************** START *************************’)
while True:
LedPower.off
sleep_us(samplingTime)
voMeasured = measurePIN.read() # read Dust Value
sleep_us(deltaTime)
LedPower.on
sleep_us(sleepTime)
print(‘***************** 1111111 *************************’)
#0 – 3,3 V mapped to 0 – 4095
calcVoltage = voMeasured * (3.3 / 4096)
dustDensity = 0.17 * calcVoltage – 0.01
print(“Raw Signal Value (0-4095): {0:3.2f}”.format(voMeasured))
print(” – Voltage: {0:3.2f}”.format(calcVoltage))
print(” – Dust Density: {0:3.2f}”.format(dustDensity))
sleep(1)
print(‘***************** ENDE *************************’)
Hi.
I’ve tested your code with a potentiometer to mimic an analog sensor and it is working fine.
Where do you get zeros? For the raw input voltage? Or for the dust value?
Hi Sara,
measurePIN has always a value of zero.
I have the code also in A>rduino Sketch and that works fine.
Do you have any idea?
That’s a very weird behavior. If it works well with Arduino IDE, it should also work with MicroPython because your code works well and that pin works well too.
I have no idea why that is happening…
Did you try using another pin? Do you get the same behavior?
Could it be that the behaviour regarding the timing is different between Micropython and Sketch (spleep() / delay())?
I don’t think it is related with that…
I’m out of ideas on what may be causing your issue :/
I found now the reason. It was a stupid syntax error (I forgot Pin.OUT for the LEDPin).
Sorry to have you contacted for that and thank you for your help and advice.
Norbert