Reputation: 2972
I was trying to test a LDR using gobot framework. I used AnalogSensorDriver device driver and my code is
package main
import (
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/aio"
"gobot.io/x/gobot/platforms/raspi"
)
func main() {
r := raspi.NewAdaptor()
ldr := aio.AnalogSensorDriver(r, "7")
work := func() {
gobot.Every(1*time.Second, func() {
ldr.Read()
})
}
robot := gobot.NewRobot("getBot",
[]gobot.Connection{r},
[]gobot.Device{ldr},
work,
)
robot.Start()
}
When I execute this, I am getting this error.
./ldrtest.go:13: too many arguments to conversion to aio.AnalogSensorDriver: aio.AnalogSensorDriver(w, "7") ./ldrtest.go:22: undefined: w
I am totally new for golang and gobot. So any kind of help to solve this problem would be greatly appreciated.
Thanks in advance.
Upvotes: 0
Views: 1472
Reputation: 61
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:
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()
}
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.
Upvotes: 1
Reputation: 38343
From a quick glance at gobot
's source it seems to me that you cannot use aio.NewAnalogSensorDriver
with the raspi.Adaptor
. The aio.NewAnalogSensorDriver expects it's first argument to be an interface
of type AnalogReader while the raspi.NewAdaptor returns a raspi.Adaptor that, seemingly, does not implement the AnalogRead method required for it to implement the AnalogReader
interface.
That's why you get that error ./ldrtest.go:13: cannot use r (type *raspi.Adaptor) as type aio.AnalogReader in argument to aio.NewAnalogSensorDriver: *raspi.Adaptor does not implement aio.AnalogReader (missing AnalogRead method)
.
Update:
Making your code work depends on what you want to do. If you want to use raspi.Adaptor
you cannot use aio.NewAnalogSensorDriver
because the raspi.Adaptor
does not have analog capabilities. If you want to use the aio.NewAnalogSensorDriver
you'll need to use, as its first argument, a value whose type implements the AnalogRead
method, like for example the beaglebone.Adaptor does.
package main
import (
"time"
"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/aio"
"gobot.io/x/gobot/platforms/beaglebone"
)
func main() {
r := beaglebone.NewAdaptor()
ldr := aio.NewAnalogSensorDriver(r, "7")
// ...
}
This example should get you past that initial error, if the code below causes other issues you should consider consulting the documentation for both Go and gobot.
Upvotes: 1