Still in a bit of shock that the PI can not easily do any analog inputs (analogRead) and instead expects you to use I2C style boards. My problem is I have several photoresistors, flex strips and pressure sensitive resistors for my classroom. I do not need accurate measurements I just need something that works.
Here are my sensors:
I found a previous hack at https://pimylifeup.com/raspberry-pi-light-sensor/ but I have found this easier to understand trick at http://www.raspberrypi-spy.co.uk/2012/08/reading-analogue-sensors-with-one-gpio-pin/ which is the same kind of thing using a capacitor to measure its charge time but I like this one better. The steps are:
-
Set the GPIO pin as an output and set it Low. This discharges any charge in the capacitor and ensures that both sides of the capacitor are 0V.
-
Set the GPIO pin as an input. This starts a flow of current through the resistors and through the capacitor to ground. The voltage across the capacitor starts to rise. The time it takes is proportional to the resistance of the LDR.
-
Monitor the GPIO pin and read its value. Increment a counter while we wait.
-
At some point the capacitor voltage will increase enough to be considered as a High by the GPIO pin (approx 2v). The time taken is proportional to the light level seen by the LDR.
-
Set the GPIO pin as an output and repeat the process as required.
I can probably figure out the code, but if anyone has already done it that would be useful. I am presently busy working on: a case, a web interface, and serial communication using I2C, SPI and UART with the PI.