11

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.

Matthew Rankin
  • 457,139
  • 39
  • 126
  • 163
user2725228
  • 161
  • 1
  • 5

5 Answers5

5

Check out Dave Cheney's package:

There's a classical blink example there.

Gustavo Niemeyer
  • 22,007
  • 5
  • 57
  • 46
  • 6
    This repository [is no longer actively maintained](https://github.com/davecheney/gpio/issues/21#issuecomment-158212861). – Nathan Osman Nov 22 '15 at 02:52
3

http://embd.kidoman.io/

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

ben schwartz
  • 2,559
  • 1
  • 21
  • 20
3

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.

Nathan Osman
  • 71,149
  • 71
  • 256
  • 361
1

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.

Matthew Rankin
  • 457,139
  • 39
  • 126
  • 163
  • 1
    The Linux kernel maintainers recommend the `gpiod` by Kent (second in the list). Do not forget the portability aspect, which most of *Pi packagers lack of. – 0andriy Aug 13 '23 at 11:37
0

Another is goPi - also has support for piface

And the blink examples

PedroMorgan
  • 926
  • 12
  • 15