drivers

package module
v0.34.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2025 License: BSD-3-Clause Imports: 2 Imported by: 279

README

TinyGo Drivers

PkgGoDev Build

This package provides a collection of over 130 different hardware drivers for devices such as sensors, displays, wireless adaptors, and actuators, that can be used together with TinyGo.

For the complete list, please see: https://tinygo.org/docs/reference/devices/

Installing

go get tinygo.org/x/drivers

How to use

Here is an example in TinyGo that uses the BMP180 digital barometer. This example should work on any board that supports I2C:

package main

import (
    "time"

    "machine"

    "tinygo.org/x/drivers/bmp180"
)

func main() {
    machine.I2C0.Configure(machine.I2CConfig{})
    sensor := bmp180.New(machine.I2C0)
    sensor.Configure()

    connected := sensor.Connected()
    if !connected {
        println("BMP180 not detected")
        return
    }
    println("BMP180 detected")

    for {
        temp, _ := sensor.ReadTemperature()
        println("Temperature:", float32(temp)/1000, "°C")

        pressure, _ := sensor.ReadPressure()
        println("Pressure", float32(pressure)/100000, "hPa")

        time.Sleep(2 * time.Second)
    }
}

Examples Using GPIO or SPI

If compiling these examples directly you are likely to need to make minor changes to the defined variables to map the pins for the board you are using. For example, this block in main.go:

var (
        spi   = machine.SPI0
        csPin = machine.D5
)

It might not be obvious, but you need to change these to match how you wired your specific board. Constants are defined for each supported microcontroller.

For example, to change the definitions for use on a Raspberry Pi Pico using typical wiring, you might need to do this:

var (
        spi   = machine.SPI0
        csPin = machine.GP17
)

Contributing

Your contributions are welcome!

Please take a look at our CONTRIBUTING.md document for details.

License

This project is licensed under the BSD 3-clause license, just like the Go project itself.

Documentation

Overview

Package drivers provides a collection of hardware drivers for TinyGo (https://tinygo.org) for devices such as sensors and displays.

Here is an example in TinyGo that uses the BMP180 digital barometer:

package main

import (
	"time"
	"machine"

	"tinygo.org/x/drivers/bmp180"
)

func main() {
	machine.I2C0.Configure(machine.I2CConfig{})
	sensor := bmp180.New(machine.I2C0)
	sensor.Configure()

	connected := sensor.Connected()
	if !connected {
		println("BMP180 not detected")
		return
	}
	println("BMP180 detected")

	for {
		temp, _ := sensor.ReadTemperature()
		println("Temperature:", float32(temp)/1000, "°C")

		pressure, _ := sensor.ReadPressure()
		println("Pressure", float32(pressure)/100000, "hPa")

		time.Sleep(2 * time.Second)
	}
}

Each individual driver is contained within its own sub-package within this package and there are no interdependencies in order to minimize the final size of compiled code that uses any of these drivers.

Index

Constants

View Source
const (
	Rotation0 = iota
	Rotation90
	Rotation180
	Rotation270
	Rotation0Mirror
	Rotation90Mirror
	Rotation180Mirror
	Rotation270Mirror
)

Clockwise rotation of the screen.

View Source
const Version = "0.34.0"

Version returns a user-readable string showing the version of the drivers package for support purposes. Update this value before release of new version of software.

Variables

This section is empty.

Functions

This section is empty.

Types

type Displayer

type Displayer interface {
	// Size returns the current size of the display.
	Size() (x, y int16)

	// SetPizel modifies the internal buffer.
	SetPixel(x, y int16, c color.RGBA)

	// Display sends the buffer (if any) to the screen.
	Display() error
}

type I2C added in v0.14.0

type I2C interface {
	// Tx performs a [I²C] transaction with address addr.
	// Most I2C peripherals have some sort of register mapping scheme to allow
	// users to interact with them:
	//
	//  bus.Tx(addr, []byte{reg}, buf) // Reads register reg into buf.
	//  bus.Tx(addr, append([]byte{reg}, buf...), nil) // Writes buf into register reg.
	//
	// The semantics of most I2C transactions require that the w write buffer be non-empty.
	//
	// [I²C]: https://en.wikipedia.org/wiki/I%C2%B2C
	Tx(addr uint16, w, r []byte) error
}

I2C represents an I2C bus. It is notably implemented by the machine.I2C type.

type Measurement added in v0.25.0

type Measurement uint32

Measurement specifies a type of measurement, for example: temperature, acceleration, pressure.

const (
	Voltage Measurement = 1 << iota
	Temperature
	Humidity
	Pressure
	Distance
	Acceleration
	AngularVelocity
	MagneticField
	Luminosity
	Time
	// Gas or liquid concentration, usually measured in ppm (parts per million).
	Concentration

	// AllMeasurements is the OR of all Measurement values. It ensures all measurements are done.
	AllMeasurements Measurement = (1 << 32) - 1
)

Sensor measurements

type Rotation added in v0.25.0

type Rotation uint8

Rotation is how much a display has been rotated. Displays can be rotated, and sometimes also mirrored.

type SPI added in v0.15.1

type SPI interface {
	// Tx transmits the given buffer w and receives at the same time the buffer r.
	// The two buffers must be the same length. The only exception is when w or r are nil,
	// in which case Tx only transmits (without receiving) or only receives (while sending 0 bytes).
	Tx(w, r []byte) error

	// Transfer writes a single byte out on the SPI bus and receives a byte at the same time.
	// If you want to transfer multiple bytes, it is more efficient to use Tx instead.
	Transfer(b byte) (byte, error)
}

SPI represents a SPI bus. It is implemented by the machine.SPI type.

type Sensor added in v0.25.0

type Sensor interface {
	// Update performs IO to update the measurements of a sensor.
	// It shall return error only when the sensor encounters an error that prevents it from
	// storing all or part of the measurements it was called to do.
	Update(which Measurement) error
}

Sensor represents an object capable of making one or more measurements. A sensor will then have methods which read the last updated measurements.

Many Sensors may be collected into one Sensor interface to synchronize measurements.

type UART added in v0.16.0

type UART interface {
	io.Reader
	io.Writer

	Buffered() int
}

UART represents a UART connection. It is implemented by the machine.UART type.

Directories

Path Synopsis
Package adafruit4650 implements a driver for the Adafruit FeatherWing OLED - 128x64 OLED display.
Package adafruit4650 implements a driver for the Adafruit FeatherWing OLED - 128x64 OLED display.
Package adt7410 provides a driver for the adt7410 I2C Temperature Sensor.
Package adt7410 provides a driver for the adt7410 I2C Temperature Sensor.
Package adxl345 provides a driver for the ADXL345 digital accelerometer.
Package adxl345 provides a driver for the ADXL345 digital accelerometer.
Package amg88xx provides a driver for the AMG88XX Thermal Camera
Package amg88xx provides a driver for the AMG88XX Thermal Camera
Package apa102 implements a driver for the APA102 SPI LED.
Package apa102 implements a driver for the APA102 SPI LED.
Package apds9960 implements a driver for APDS-9960, a digital proximity, ambient light, RGB and gesture sensor.
Package apds9960 implements a driver for APDS-9960, a digital proximity, ambient light, RGB and gesture sensor.
Package at24cx provides a driver for the AT24C32/64/128/256/512 2-wire serial EEPROM
Package at24cx provides a driver for the AT24C32/64/128/256/512 2-wire serial EEPROM
Package axp192 provides a driver for the axp192 I2C Enhanced single Cell Li-Battery and Power System Management IC.
Package axp192 provides a driver for the axp192 I2C Enhanced single Cell Li-Battery and Power System Management IC.
Package bh1750 provides a driver for the BH1750 digital Ambient Light
Package bh1750 provides a driver for the BH1750 digital Ambient Light
Package blinkm implements a driver for the BlinkM I2C RGB LED.
Package blinkm implements a driver for the BlinkM I2C RGB LED.
Package bma42x provides a driver for the BMA421 and BMA425 accelerometer chips.
Package bma42x provides a driver for the BMA421 and BMA425 accelerometer chips.
Package bme280 provides a driver for the BME280 digital combined humidity and pressure sensor by Bosch.
Package bme280 provides a driver for the BME280 digital combined humidity and pressure sensor by Bosch.
Package bmp180 provides a driver for the BMP180 digital pressure sensor by Bosch.
Package bmp180 provides a driver for the BMP180 digital pressure sensor by Bosch.
Package bmp280 provides a driver for the BMP280 digital temperature & pressure sensor by Bosch.
Package bmp280 provides a driver for the BMP280 digital temperature & pressure sensor by Bosch.
Package bmp388 provides a driver for Bosch's BMP388 digital temperature & pressure sensor.
Package bmp388 provides a driver for Bosch's BMP388 digital temperature & pressure sensor.
Package bno08x provides a TinyGo driver for the Adafruit BNO08x 9-DOF IMU sensors.
Package bno08x provides a TinyGo driver for the Adafruit BNO08x 9-DOF IMU sensors.
Package buzzer provides a very simplistic driver for a connected buzzer or low-fidelity speaker.
Package buzzer provides a very simplistic driver for a connected buzzer or low-fidelity speaker.
cmd
convert2bin command
Package ds1307 provides a driver for the DS1307 RTC
Package ds1307 provides a driver for the DS1307 RTC
Package ds18b20 provides a driver for the DS18B20 digital thermometer
Package ds18b20 provides a driver for the DS18B20 digital thermometer