bh1750

package
v3.6.3+incompatible Latest Latest
Warning

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

Go to latest
Published: May 29, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package bh1750 controls a ROHM BH1750 ambient light sensor, over an i2c bus.

Datasheet

http://cpre.kmutnb.ac.th/esl/learning/bh1750-light-sensor/bh1750fvi-e_datasheet.pdf

Example
package main

import (
	"fmt"
	"log"

	"periph.io/x/periph/conn/i2c/i2creg"
	"periph.io/x/periph/experimental/devices/bh1750"
	"periph.io/x/periph/host"
)

func main() {
	// Make sure periph is initialized.
	if _, err := host.Init(); err != nil {
		log.Fatal(err)
	}

	// Open default I²C bus.
	bus, err := i2creg.Open("")
	if err != nil {
		log.Fatalf("failed to open I²C: %v", err)
	}
	defer bus.Close()

	// Create a new light sensor.
	sensor, err := bh1750.NewI2C(bus, bh1750.I2CAddr)
	if err != nil {
		log.Fatalln(err)
	}

	// Read value from sensor.
	measurement, err := sensor.Sense()

	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(measurement)
}
Output:

Index

Examples

Constants

View Source
const AlternativeI2CAddr uint16 = 0x5c

AlternativeI2CAddr i2c alternative address.

View Source
const I2CAddr uint16 = 0x23

I2CAddr i2c default address.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dev

type Dev struct {
	// contains filtered or unexported fields
}

Dev is a handler to bh1750 controller

func NewI2C

func NewI2C(bus i2c.Bus, address uint16) (*Dev, error)

NewI2C opens a handle to an bh1750 sensor.

To use on the default address, bh1750.I2CAddr must be passed as argument.

func (*Dev) Halt

func (d *Dev) Halt() error

Halt turn off device.

func (*Dev) Sense

func (d *Dev) Sense() (physic.LuminousFlux, error)

Sense reads the light value from the bh1750 sensor.

func (*Dev) SetMode

func (d *Dev) SetMode(m Mode) error

SetMode set the sleep mode of the sensor.

func (*Dev) SetResolution

func (d *Dev) SetResolution(r Resolution) error

SetResolution set the resolution mode of the sensor.

type Mode

type Mode uint8

Mode - device mode.

const (
	// PowerDown - no active state.
	PowerDown Mode = 0x00

	// PowerOn - waiting for measurement command.
	PowerOn Mode = 0x01
)

type Resolution

type Resolution uint8

Resolution represents the measurements modes.

const (
	// ContinuousHighResMode start measurement at 1lx resolution. Measurement time is approx 120ms.
	ContinuousHighResMode Resolution = 0x10

	// ContinuousHighResMode2 start measurement at 0.5lx resolution. Measurement time is approx 120ms.
	ContinuousHighResMode2 Resolution = 0x11

	// ContinuousLowResMode start measurement at 4lx resolution. Measurement time is approx 16ms.
	ContinuousLowResMode Resolution = 0x13

	// OneTimeHighResMode measurement at 1lx resolution and the device is automatically set to Power Down after measurement.
	OneTimeHighResMode Resolution = 0x20

	// OneTimeHighResMode2 measurement at 0.5lx resolution and the device is automatically set to Power Down after measurement.
	OneTimeHighResMode2 Resolution = 0x21

	// OneTimeLowResMode measurement at 4lx resolution and the device is automatically set to Power Down after measurement.
	OneTimeLowResMode Resolution = 0x23
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL