The raspberry pi does not have a way to connect analog devices directly, so, whatever the framework, you cannot simply attach the photoresistor (LDR) directly to the board. There are 2 ways to use photoresistors in that case:
- You need an analog-digital converter (ADC), such as the MCP3008, ADS1015, ADS1115.
As of April, 30th, 2017, Gobot.io supports the ADS1015/ADS1115 in the Dev branch. If you connect your device to an ADS1015 on the channel 0, then the converter to the I2C interface of the raspberry pi, the code would look like that:
package main
import (
"fmt"
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/platforms/raspi"
)
func main() {
a := raspi.NewAdaptor()
ads1015 := i2c.NewADS1015Driver(a)
// Adjust the gain to be able to read values of at least 5V
ads1015.DefaultGain, _ = ads1015.BestGainForVoltage(5.0)
work := func() {
gobot.Every(100*time.Millisecond, func() {
v, _ := ads1015.ReadWithDefaults(0)
fmt.Println("A0", v)
})
}
robot := gobot.NewRobot("ads1015bot",
[]gobot.Connection{a},
[]gobot.Device{ads1015},
work,
)
robot.Start()
}
- There is a solution to use such sensors without ADC converters, described here:
Because photocells are basically resistors, its possible to use them even if you don't have any analog pins on your microcontroller (or if say you want to connect more than you have analog input pins). The way we do this is by taking advantage of a basic electronic property of resistors and capacitors. It turns out that if you take a capacitor that is initially storing no voltage, and then connect it to power (like 5V) through a resistor, it will charge up to the power voltage slowly. The bigger the resistor, the slower it is.
You can find many examples on the internet on how to connect the LDR in such case. I'm not sure there is a working example with Gobot.io right now.