Skip to content

makers-bierzo/sds011

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-sds011

Go library to control a SDS011 laser dust sensor.

Example

First, open serial port

c := &serial.Config{Name: "/dev/ttyUSB0", Baud: 9600}
s, _ := serial.OpenPort(c)

Create a sds011 sensor instance over this connection

sensor := sds011.NewSensor(s)

Disable sleep mode to take samples

_ = sensor.Sleep(false)

Select sensor mode. On QueryMode, sensor only read when you request a reading

_ = sensor.SetMode(sds011.QueryMode)

You should wait a little while for the fan to collect an air sample

time.Sleep(15 * time.Second)

Read and show the measure

measure, _ := sensor.Query()
fmt.Printf("PM 2.5 => %f μg/m³\nPM 10 => %f μg/m³\n", measure.PM2_5, measure.PM10)

You can change sensor to ActiveMode. In this mode the sensor takes a sample periodically, in this case, every 1 minute.

_ = sensor.SetMode(sds011.ActiveMode)
_ = sensor.SetWorkingPeriod(1)

To receive the samples, when they are read, you must listen new values from channel of query responses.

measureChannel := make(chan sds011.Measurement)

sensor.OnQuery(measureChannel)
sensor.Listen()

for true {
    measure := <- measureChannel
    fmt.Printf("PM 2.5 => %f μg/m³\nPM 10 => %f μg/m³\n", measure.PM2_5, measure.PM10)
}

About

Go library to control a SDS011 laser dust sensor

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages