How can I read the temperature sensor values on GPIO of a Raspberry Pi using the go language?
Please, anyone help me out.
Thanks in advance.
How can I read the temperature sensor values on GPIO of a Raspberry Pi using the go language?
Please, anyone help me out.
Thanks in advance.
Check out Dave Cheney's package:
There's a classical blink example there.
this is a slightly higher level abstraction than dave cheney's gpio library.
In addition to a gpio api, there is support for many common sensors
not sure what your sensor is, but e.g. here is an example for the bmp180 barometric sensor
I have created an extremely simple package for interacting with the GPIO pins on a Raspberry Pi:
https://github.com/nathan-osman/go-rpigpio
A simple program that makes pin 2 flash ten times would look something like this:
package main
import (
"github.com/nathan-osman/go-rpigpio"
"time"
)
func main() {
p, err := rpi.OpenPin(2, rpi.OUT)
if err != nil {
panic(err)
}
defer p.Close()
for i := 0; i < 10; i++ {
p.Write(rpi.HIGH)
time.Sleep(300 * time.Millisecond)
p.Write(rpi.LOW)
time.Sleep(100 * time.Millisecond)
}
}
More documentation can be found here.
There are quite a few Go modules to access the GPIO pins on a Raspberry Pi. However, not all of them are regularly maintained. The table below summarizes some of the available Go modules as of August 11, 2023. The table is sorted by last commit date.
Module | Stars | Last Commit | License | Notes |
---|---|---|---|---|
hybridgroup/gobot | 8.4k | Jul 2023 | Apache2 | Framework for robotics/IoT |
warthog618/gpiod | 321 | Jul 2023 | MIT | |
periph/conn | 48 | Apr 2023 | Apache-2.0 | Part of periph library |
warthog618/gpio | 114 | Dec 2022 | MIT | Relies on sysfs which is deprecated |
stianeikeland/go-rpio | 2k | Dec 2021 | MIT | |
mrmorphic/hwio | 322 | May 2018 | BSD-3 | |
kidoman/embd | 1.3k | May 2017 | MIT | |
nathan-osman/go-rpigpio | 64 | Jun 2016 | MIT | |
luismesas/goPi | 74 | Apr 2014 | MIT |
Reading a temperature sensor depends not just on the chosen Go module but also on the temperature sensor being used. For instance, you could use the AM2302 (wired DHT22) temperature-humidity sensor from Adafruit in which case you could use the go-dht module, which provides DHT22, AM2302, and DHT11 interfaces using the periph driver.