denko 0.13.3 → 0.13.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '082654ef6ccaec931c9cba643db7c528cc8b20d33e969af6a101a735078b7c02'
4
- data.tar.gz: 780397a81acfa1a44591deb0c1beedb6f39071b606fb108914c79af334e47847
3
+ metadata.gz: e165f677a5af1dc4d7a924e7b2387bafaca1c84c754b72eac8960fcea42117a7
4
+ data.tar.gz: f1c795bff6917284d3b31fbff4d1ea44de78f2871492b466f45d9f36abc340af
5
5
  SHA512:
6
- metadata.gz: 58e7ec2753368aa202a63792c6f142bd150a386c95d37605ee4c1985022d4c06e552108f504675c599effcde0e0438b7d1029d0c700ec7fee40fffdffc60d1f4
7
- data.tar.gz: fec058bef713c8f5c7e84eed3dd87a84db93267e921abb27d3c4966b40bf66a223a3a76442559742bec04a58665e18d3d8d30cd3954fdb846148e5d1fb3b0cd2
6
+ metadata.gz: c897cca6c4a59ce2fbd73e517c380ba272ad0c6a621a9ba7034dffe1f83e151c21cad0671df872443eff8376ff646933a0a68cda07e25860869a06a9306fcfac
7
+ data.tar.gz: 6f90da47a6bb960400d9986668b2f9320248616f0a4f0a12a930e9ffda4dad0d75e1c08ecf8aae223f8882b72bd56e73dfe113a79c3973ff91c326c4ed31f941
data/CHANGELOG.md CHANGED
@@ -1,5 +1,44 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.13.4
4
+
5
+ ### New Components
6
+
7
+ - Generic PIR sensors:
8
+ - Class: `Denko::Sensor::GenericPIR`
9
+ - Based on `Denko::DigitalIO::Input` class.
10
+ - Tested with AS312 and HC-SR501 sensors. Should work with AM312 and others.
11
+
12
+ - Bosch BMP 180 Temperature + Pressure Sensor:
13
+ - Class: `Denko::Sensor::BMP180`
14
+ - Connects via I2C bus. Ruby driver.
15
+ - Similar to BMP280, but fewer features (older version).
16
+ - Should work for BMP085 sensor as well.
17
+
18
+ - SHT30/31/35 Temperature + Humidity Sensor:
19
+ - Class: `Denko::Sensor::SHTX`
20
+ - Connects via I2C bus. Ruby driver.
21
+ - One-shot reading mode only.
22
+
23
+ - RCWL-9620 Ultrasonic Distance Sensor:
24
+ - Class: `Denko::Sensor::RCWL9620`
25
+ - Connects via I2C bus. Ruby driver.
26
+ - Very simple interface. `#read` returns a distance in mm.
27
+
28
+ ### Component Changes
29
+
30
+ - HTU21D:
31
+ - Humidity values outside the 0-100% range will be clipped to those values automatically.
32
+
33
+ ### Example Changes
34
+
35
+ - Added a simple button example, separate from tutorial.
36
+ - Standardize temp/pressure/humidity sensor examples (except DHT, DS18B20, HTU21D) so readings display the same.
37
+
38
+ ### Bug Fixes
39
+
40
+ - Fixed bug where `BMP280`` sensor class would not autoload.
41
+
3
42
  ## 0.13.3
4
43
 
5
44
  ### Board Updates / Fixes
data/HARDWARE.md CHANGED
@@ -37,6 +37,12 @@
37
37
  | ATWINC1500 | :green_heart: | MKR1000, WiFi Shield 101 | #define WIFI_101 for shield. Automatic for MKR1000
38
38
  | u-blox NINA-W102 | :question: | Uno WiFi Rev 2, MKR WiFi 1010, Nano 33 IOT | Should work. No hardware
39
39
 
40
+ ### AVR Chips from [MightyCore](https://github.com/MCUdude/MightyCore)
41
+
42
+ | Chip | Status | Products | Notes |
43
+ | :-------- | :------: | :--------------- |------ |
44
+ | ATmega1284 | :heart: | Used in many 8-bit 3D printer boards. |
45
+
40
46
  ### Espressif Chips with Built-In Wi-Fi
41
47
  [![ESP8266 Build Status](https://github.com/denko-rb/denko/actions/workflows/build_esp8266.yml/badge.svg)](https://github.com/denko-rb/denko/actions/workflows/build_esp8266.yml)
42
48
  [![ESP32 Build Status](https://github.com/denko-rb/denko/actions/workflows/build_esp32.yml/badge.svg)](https://github.com/denko-rb/denko/actions/workflows/build_esp32.yml)
@@ -119,6 +125,7 @@ Polling and reading follow a call and response pattern.
119
125
  | LED | :green_heart: | Digi/Ana Out | `LED::Base` |
120
126
  | RGB LED | :green_heart: | Digi/Ana Out | `LED::RGB` |
121
127
  | 7 Segment Display | :yellow_heart: | Digital Out | `LED::SevenSegment` | No decimal point
128
+ | 8x8 LED (MAX7219) | :heart: | SPI | `LED::MAX7219` |
122
129
  | TM1637 | :heart: | BitBang SPI | `LED::TM1637` | 4x 7 Segment + Colon
123
130
  | Neopixel / WS2812B | :yellow_heart: | Adafruit Library | `LED::WS2812` | Not working on RP2040
124
131
  | Dotstar / APA102 | :green_heart: | SPI | `LED::APA102` |
@@ -129,6 +136,12 @@ Polling and reading follow a call and response pattern.
129
136
  | :--------------- | :------: | :-------- | :--------------- |------ |
130
137
  | HD44780 LCD | :green_heart: | Digital Out, Output Register | `Display::HD44780` |
131
138
  | SSD1306 OLED | :yellow_heart: | I2C | `Display::SSD1306` | 1 font, some graphics
139
+ | ST7565R (128x64 Mono) | :heart: | SPI | `Display::ST7565R` |
140
+ | ST7735S (160x128 RGB) | :heart: | SPI | `Display::ST7735S` |
141
+ | ILI9341 (240x320 RGB) | :heart: | SPI | `Display::ILI9341` |
142
+ | GC9107 (128x128 RGB) | :heart: | SPI | `Display::GC9107` |
143
+ | GC9A01 (240x240 Round) | :heart: | SPI | `Display::GCA9A01` |
144
+ | IL0373 (212x104 E-Paper) | :heart: | SPI | `Display::IL0373` |
132
145
 
133
146
  ### Sound
134
147
 
@@ -144,7 +157,9 @@ Polling and reading follow a call and response pattern.
144
157
  | Generic ESC | :yellow_heart: | Servo/ESC PWM | `Motor::Servo` | Works. Needs its own class.
145
158
  | PCA9685 | :heart: | I2C | `PulseIO::PCA9685` | 16ch 12-bit PWM for servo or LED
146
159
  | L298N | :green_heart: | Digi + PWM Out | `Motor::L298` | H-Bridge DC motor driver
160
+ | DRV8833 | :heart: | Digi + PWM Out | `Motor::DRV8833` | H-Bridge DC motor driver
147
161
  | A3967 | :green_heart: | Digital Out | `Motor::Stepper` | 1ch microstepper (EasyDriver)
162
+ | A4988 | :yellow_heart: | DigitalOut | `Motor::Stepper` | 1-ch microstepper
148
163
  | TMC2209 | :heart: | - | - | 1ch silent stepper driver
149
164
 
150
165
  ### I/O Expansion
@@ -162,55 +177,67 @@ Polling and reading follow a call and response pattern.
162
177
 
163
178
  ### Environmental Sensors
164
179
 
165
- | Name | Status | Interface | Component Class | Notes |
166
- | :--------------- | :------: | :-------- | :--------------- |------ |
167
- | DS18B20 | :green_heart: | OneWire | `Sensor::DS18B20` | Temp
168
- | DHT 11/21/22 | :green_heart: | Digi In/Out | `Sensor::DHT` | Temp/RH
169
- | SHT30 | :heart: | I2C | `Sensor::SHT30` | Temp/RH
170
- | QMP6988 | :heart: | I2C | `Sensor::QMP6988` | Pressure
171
- | BME280 | :green_heart: | I2C | `Sensor::BME280` | Temp/RH/Press
172
- | BMP280 | :green_heart: | I2C | `Sensor::BMP280` | Temp/Press
173
- | HTU21D | :green_heart: | I2C | `Sensor::HTU21D` | Temp/RH. User register read not implemented.
174
- | HTU31D | :green_heart: | I2C | `Sensor::HTU31D` | Temp/RH. Diagnostic read not implemented.
175
- | AHT10/15 | :green_heart: | I2C | `Sensor::AHT10` | Temp/RH. Always uses calibrated mode.
176
- | AHT20/21/25 | :green_heart: | I2C | `Sensor::AHT20` | Temp/RH. Always uses calibrated mode + CRC.
177
- | ENS160 | :heart: | I2C | `Sensor::ENS160` | CO2e/TVOC/AQI
178
- | AGS02MA | :heart: | I2C | `Sensor::AGS02MA` | TVOC
179
- | MAX31850 | :heart: | OneWire | `Sensor::MAX31850` | Thermocouple Amplifier
180
+ | Name | Status | Interface | Component Class | Type | Notes |
181
+ | :--------------- | :------: | :-------- | :--------------- |--------------- | ---------------------- |
182
+ | MAX31850 | :heart: | OneWire | `Sensor::MAX31850` | Thermocouple |
183
+ | MAX6675 | :heart: | SPI | `Sensor::MAX6675` | Thermocouple |
184
+ | DS18B20 | :green_heart: | OneWire | `Sensor::DS18B20` | Temp |
185
+ | DHT11/21/22 | :green_heart: | Digi In/Out | `Sensor::DHT` | Temp / RH |
186
+ | SHT30/31/35 | :green_heart: | I2C | `Sensor::SHT3X` | Temp / RH | M5Stack ENV III, one-shot only
187
+ | QMP6988 | :green_heart: | I2C | `Sensor::QMP6988` | Temp / Press | M5Stack ENV III
188
+ | BMP180 | :green_heart: | I2C | `Sensor::BMP180` | Temp / Press |
189
+ | BMP280 | :green_heart: | I2C | `Sensor::BMP280` | Temp / Press |
190
+ | BME280 | :green_heart: | I2C | `Sensor::BME280` | Temp / Press / RH |
191
+ | HTU21D | :green_heart: | I2C | `Sensor::HTU21D` | Temp / RH | No user register read
192
+ | HTU31D | :green_heart: | I2C | `Sensor::HTU31D` | Temp / RH | No diagnostic read
193
+ | AHT10/15 | :green_heart: | I2C | `Sensor::AHT10` | Temp / RH |
194
+ | AHT20/21/25 | :green_heart: | I2C | `Sensor::AHT20` | Temp / RH |
195
+ | ENS160 | :heart: | I2C | `Sensor::ENS160` | CO2e / TVOC / AQI |
196
+ | AGS02MA | :heart: | I2C | `Sensor::AGS02MA` | TVOC |
197
+ | SCD40 | :heart: | I2C | `Sensor::SDC40` | Temp / Press / CO2 |
180
198
 
181
199
  ### Light Sensors
182
200
 
183
201
  | Name | Status | Interface | Component Class | Notes |
184
202
  | :--------------- | :------: | :-------- | :--------------- |------ |
185
203
  | BH1750 | :heart: | Digital In | `Sensor::BH1750` | Ambient Light
186
- | HC-SR501 | :yellow_heart: | Digital In | `DigitalIO::Input` | PIR. Needs class: `Sensor::HC-SR501`
187
- | AS312 | :heart: | I2C | `Sensor::AS312` | PIR
204
+ | TCS34725 | :heart: | I2C | `Sensor::TCS34725` | RGB
188
205
  | APDS9960 | :heart: | I2C | `Sensor::APDS9960` | Proximity, RGB, Gesture
189
206
 
207
+ ### PIR Motion Sensors
208
+ | Name | Status | Interface | Component Class | Notes |
209
+ | :--------------- | :------: | :-------- | :--------------- |------ |
210
+ | HC-SR501 | :green_heart: | Digital In | `Sensor::GenericPIR` |
211
+ | AS312 | :green_heart: | Digital In | `Sensor::GenericPIR` |
212
+
190
213
  ### Distance Sensors
191
214
 
192
215
  | Name | Status | Interface | Component Class | Notes |
193
216
  | :--------------- | :------: | :-------- | :--------------- |------ |
194
217
  | HC-SR04 | :heart: | Digi In/Out | `Sensor::HCSR04` | Ultrasonic, 20-4000mm
195
- | RCWL-9620 | :heart: | I2C | `Sensor::RCWL9260` | Ultrasonic, 20-4500mm
218
+ | RCWL-9620 | :green_heart: | I2C | `Sensor::RCWL9260` | Ultrasonic, 20-4500mm
196
219
  | VL53L0X | :heart: | I2C | `Sensor::VL53L0X` | Laser, 30 - 1000mm
197
220
  | GP2Y0E03 | :heart: | I2C | `Sensor::GP2Y0E03` | Infrared, 40 - 500mm
198
221
 
199
- ### Motion Sensors
222
+ ### Inertial Measurement Units
200
223
 
201
224
  | Name | Status | Interface | Component Class | Notes |
202
225
  | :--------------- | :------: | :-------- | :--------------- |------ |
203
- | ADXL345 | :heart: | I2C | `Sensor::ADXL345` | 3-axis Accelerometer
204
- | IT3205 | :heart: | I2C | `Sensor::IT3205` | 3-axis Gyroscope
205
- | HMC5883L | :heart: | I2C | `Sensor::HMC5883L` | 3-axis Compass
206
- | MPU6886 | :heart: | I2C | `Sensor::MPU6886` | 3-axis Gyro + Accel
226
+ | ADXL345 | :heart: | I2C | `Sensor::ADXL345` | Accelerometer
227
+ | IT3205 | :heart: | I2C | `Sensor::IT3205` | Gyroscope
228
+ | HMC5883L | :heart: | I2C | `Sensor::HMC5883L` | Compass
229
+ | MPU6886 | :heart: | I2C | `Sensor::MPU6886` | Gyro + Accelerometer
230
+ | BMI160 | :heart: | I2C | `Sensor::BMI160` | Gyro + Accelerometer
231
+ | LSM6DS3 | :heart: | I2C | `Sensor:LSM6DS3` | Gyro + Accelerometer
207
232
 
208
233
  ### Real Time Clocks
209
234
 
210
235
  | Name | Status | Interface | Component Class | Notes |
211
236
  | :--------------- | :------: | :-------- | :--------------- |------ |
212
237
  | DS1302 | :heart: | I2C | `RTC::DS1302` |
238
+ | DS1307 | :heart: | I2C | `RTC::DS1307` |
213
239
  | DS3231 | :green_heart: | I2C | `RTC::DS3231` | Alarms not implemented
240
+ | PCF8563 | :heart: | I2C | `RTC::PCF8563` |
214
241
 
215
242
  ### GPS
216
243
 
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Denko 0.13.3 [![Test Status](https://github.com/denko-rb/denko/actions/workflows/ruby.yml/badge.svg)](https://github.com/denko-rb/denko/actions/workflows/ruby.yml)
1
+ # Denko 0.13.4 [![Test Status](https://github.com/denko-rb/denko/actions/workflows/ruby.yml/badge.svg)](https://github.com/denko-rb/denko/actions/workflows/ruby.yml)
2
2
  ### Ruby Meets Microcontrollers
3
3
  Denko gives you a high-level Ruby interface to low-level hardware, without writing microcontroller code. Use LEDs, buttons, sensors and more, just as easily as any Ruby object:
4
4
 
@@ -23,10 +23,10 @@ High-level abstraction in Ruby makes hardware classes easy to implement, with in
23
23
  Full list of supported mircocontroller platforms, interfaces, and peripherals is located [here](HARDWARE.md).
24
24
 
25
25
  ##### denko-piboard
26
- There's an add-on in development, [denko-piboard](https://github.com/denko-rb/denko-piboard), which supports the Raspberry Pi's built in GPIO pins, as `Denko::PiBoard`, class-compatible with `Denko::Board`. Connect peripherals directly to the Pi, with no microcontroller between, then use the peripheral classes from this gem.
26
+ The add-on gem, [denko-piboard](https://github.com/denko-rb/denko-piboard), allows you to use a Raspberry Pi's built in GPIO pins in place of an attached microcontroller. Connect things directly to the Pi, and use the same peripheral classes from this gem.
27
27
 
28
28
  ##### mruby-denko
29
- A solo Raspberry Pi (or other small SBC + microcontroller) is a great standalone setup if your project needs the compute power anyway, but what if you don't? Why not run Ruby on the microcontroller itself?
29
+ A solo Raspberry Pi (or other small SBC + microcontroller) is a great standalone setup if your project needs the compute power anyway, but what if you don't? Why not run Ruby on the microcontroller itself?
30
30
 
31
31
  That's the goal of [mruby-denko](https://github.com/denko-rb/mruby-denko): write mruby on the ESP32, using peripheral classes as close to this gem as possible. Still early in development, so limited features, but already usable.
32
32
 
@@ -0,0 +1,48 @@
1
+ #
2
+ # This example combines the SHTX and QMP6988 examples. The M5Stack ENV III unit
3
+ # contains both sensors, accessible over a single I2C connection.
4
+ #
5
+ require 'bundler/setup'
6
+ require 'denko'
7
+
8
+ board = Denko::Board.new(Denko::Connection::Serial.new)
9
+ bus = Denko::I2C::Bus.new(board: board, pin: :SDA)
10
+ sht = Denko::Sensor::SHT3X.new(bus: bus) # address: 0x44 default
11
+ qmp = Denko::Sensor::QMP6988.new(bus: bus) # address: 0x70 default
12
+
13
+ # Configure for higher accuracy.
14
+ sht.repeatability = :high
15
+ qmp.temperature_samples = 2
16
+ qmp.pressure_samples = 16
17
+ qmp.iir_coefficient = 2
18
+
19
+ # Buggy on ESP32-S3 in forced mode. Data registers return zeroes on all but first read.
20
+ # Can't recreate on ESP32 V1, AVR or SAMD21. Put it in contiuous mode just in case.
21
+ qmp.continuous_mode
22
+
23
+ # Get the shared #print_tph_reading method to print readings neatly.
24
+ require_relative '../sensor/neat_tph_readings'
25
+
26
+ # How many degrees C the two temperature values can differ by before a warning.
27
+ TOLERANCE = 0.50
28
+
29
+ loop do
30
+ # Read both sensors.
31
+ qmp_reading = qmp.read
32
+ sht_reading = sht.read
33
+
34
+ # Retry immediately if either failed.
35
+ next unless (sht_reading && qmp_reading)
36
+
37
+ # Warn if large gap between temperature readings.
38
+ difference = (qmp_reading[:temperature] - sht_reading[:temperature]).abs
39
+ if (difference > TOLERANCE)
40
+ puts "WARNING: temperature values differed by more than #{TOLERANCE}\xC2\xB0C (#{difference.round(4)} \xC2\xB0C actual)"
41
+ end
42
+
43
+ # Combine values from both sensors, averaging their temperatures.
44
+ average_temperature = (qmp_reading[:temperature] + sht_reading[:temperature]) / 2.0
45
+ print_tph_reading(temperature: average_temperature, humidity: sht_reading[:humidity], pressure: qmp_reading[:pressure])
46
+
47
+ sleep 5
48
+ end
@@ -0,0 +1,13 @@
1
+ #
2
+ # Simple button example.
3
+ #
4
+ require 'bundler/setup'
5
+ require 'denko'
6
+
7
+ board = Denko::Board.new(Denko::Connection::Serial.new)
8
+ button = Denko::DigitalIO::Button.new(board: board, pin: 5, pullup: true)
9
+
10
+ button.up { puts "Button released!" }
11
+ button.down { puts "Button pressed!" }
12
+
13
+ sleep
@@ -6,10 +6,14 @@ require 'denko'
6
6
 
7
7
  board = Denko::Board.new(Denko::Connection::Serial.new)
8
8
  bus = Denko::I2C::Bus.new(board: board, pin: :SDA)
9
- aht10 = Denko::Sensor::AHT10.new(bus: bus)
9
+ sensor = Denko::Sensor::AHT10.new(bus: bus) # address: 0x38 default
10
10
 
11
- aht10.poll(2) do |reading|
12
- puts "Polled Reading: #{reading[:temperature].round(3)} \xC2\xB0C | #{reading[:humidity].round(3)} % RH"
11
+ # Get the shared #print_tph_reading method to print readings neatly.
12
+ require_relative 'neat_tph_readings'
13
+
14
+ # Poll it and print readings.
15
+ sensor.poll(5) do |reading|
16
+ print_tph_reading(reading)
13
17
  end
14
18
 
15
19
  sleep
@@ -6,10 +6,14 @@ require 'denko'
6
6
 
7
7
  board = Denko::Board.new(Denko::Connection::Serial.new)
8
8
  bus = Denko::I2C::Bus.new(board: board, pin: :SDA)
9
- aht20 = Denko::Sensor::AHT20.new(bus: bus)
9
+ sensor = Denko::Sensor::AHT20.new(bus: bus) # address: 0x38 default
10
10
 
11
- aht20.poll(2) do |reading|
12
- puts "Polled Reading: #{reading[:temperature].round(3)} \xC2\xB0C | #{reading[:humidity].round(3)} % RH"
11
+ # Get the shared #print_tph_reading method to print readings neatly.
12
+ require_relative 'neat_tph_readings'
13
+
14
+ # Poll it and print readings.
15
+ sensor.poll(5) do |reading|
16
+ print_tph_reading(reading)
13
17
  end
14
18
 
15
19
  sleep
@@ -5,28 +5,11 @@ require 'bundler/setup'
5
5
  require 'denko'
6
6
 
7
7
  board = Denko::Board.new(Denko::Connection::Serial.new)
8
-
9
- #
10
- # Default pins for the I2C0 (first) interface on most chips:
11
- #
12
- # ATmega 328p: SDA = 'A4' SCL = 'A5' - Arduino Uno, Nano
13
- # ATmega 32u4: SDA = 2 SCL = 3 - Arduino Leonardo, Pro Micro
14
- # ATmega1280 / 2560: SDA = 20 SCL = 21 - Arduino Mega
15
- # SAM3X8E: SDA = 20 SCL = 21 - Arduino Due
16
- # SAMD21G18: SDA = 20 SCL = 21 - Arduino Zero, M0, M0 Pro
17
- # ESP8266: SDA = 4 SCL = 5
18
- # ESP32: SDA = 21 SCL = 22
19
- # RP2040: SDA = 4 SCL = 5 - Raspberry Pi Pico (W)
20
- #
21
- # Only give the SDA pin of the I2C bus. SCL (clock) pin must be
22
- # connected for it to work, but we don't need to control it.
23
- #
24
8
  bus = Denko::I2C::Bus.new(board: board, pin: :SDA)
25
9
 
26
- sensor = Denko::Sensor::BME280.new(bus: bus, address: 0x76)
27
-
10
+ sensor = Denko::Sensor::BME280.new(bus: bus) # address: 0x76 default
28
11
  # Use A BMP280 with no humidity instead.
29
- # sensor = Denko::Sensor::BMP280.new(bus: bus, address: 0x76)
12
+ # sensor = Denko::Sensor::BMP280.new(bus: bus) # address: 0x76
30
13
 
31
14
  # Default reading mode is oneshot ("forced" in datasheet).
32
15
  # sensor.oneshot_mode
@@ -44,32 +27,12 @@ sensor = Denko::Sensor::BME280.new(bus: bus, address: 0x76)
44
27
  # Print raw config register bits.
45
28
  # print sensor.config_register_bits
46
29
 
47
- def display_reading(reading)
48
- # Time
49
- print "#{Time.now.strftime '%Y-%m-%d %H:%M:%S'} - "
50
-
51
- # Temperature
52
- formatted_temp = reading[:temperature].round(2).to_s.ljust(5, '0')
53
- print "Temperature: #{formatted_temp} \xC2\xB0C"
54
-
55
- # Pressure
56
- if reading[:pressure]
57
- formatted_pressure = (reading[:pressure] / 101325).round(5).to_s.ljust(7, '0')
58
- print " | Pressure #{formatted_pressure} atm"
59
- end
60
-
61
- # Humidity
62
- if reading[:humidity]
63
- formatted_humidity = reading[:humidity].round(2).to_s.ljust(5, '0')
64
- print " | Humidity #{formatted_humidity} %"
65
- end
66
-
67
- puts
68
- end
30
+ # Get the shared #print_tph_reading method to print readings neatly.
31
+ require_relative 'neat_tph_readings'
69
32
 
70
- # Poll the sensor and print readings.
33
+ # Poll it and print readings.
71
34
  sensor.poll(5) do |reading|
72
- display_reading(reading)
35
+ print_tph_reading(reading)
73
36
  end
74
37
 
75
38
  sleep
@@ -0,0 +1,22 @@
1
+ #
2
+ # Example using a BMP180 sensor over I2C, for temperature and pressure.
3
+ #
4
+ require 'bundler/setup'
5
+ require 'denko'
6
+
7
+ board = Denko::Board.new(Denko::Connection::Serial.new)
8
+ bus = Denko::I2C::Bus.new(board: board, pin: :SDA)
9
+ sensor = Denko::Sensor::BMP180.new(bus: bus) # address: 0x77 default
10
+
11
+ # Enable oversampling for the pressure sensor only (1,2,4, 8).
12
+ # sensor.pressure_samples = 8
13
+
14
+ # Get the shared #print_tph_reading method to print readings neatly.
15
+ require_relative 'neat_tph_readings'
16
+
17
+ # Poll it and print readings.
18
+ sensor.poll(5) do |reading|
19
+ print_tph_reading(reading)
20
+ end
21
+
22
+ sleep
@@ -0,0 +1,25 @@
1
+ #
2
+ # Example using a PIR motion sensor. Tested with AS312 and HC-SR501 sensors.
3
+ #
4
+ # General notes:
5
+ # - Both sensors have a few seconds "dead time" after "motion stop" (logical 0), where further
6
+ # motion will not trigger "motion start" (logical 1).
7
+ #
8
+ # HC-SR501 notes:
9
+ # - Needs some time to warm up and start working properly.
10
+ # - Set the time potentiometer to its lowest value.
11
+ # - Make sure retriggering is enabled. It might be default, but there's a jumper to solder too.
12
+ #
13
+ require 'bundler/setup'
14
+ require 'denko'
15
+
16
+ board = Denko::Board.new(Denko::Connection::Serial.new)
17
+ sensor = Denko::Sensor::GenericPIR.new(board: board, pin: 8)
18
+
19
+ sensor.on_motion_start { print "Motion detected! \r" }
20
+ sensor.on_motion_stop { print "No motion detected... \r" }
21
+
22
+ # Read initial state.
23
+ sensor.read
24
+
25
+ sleep
@@ -6,7 +6,7 @@ require 'denko'
6
6
 
7
7
  board = Denko::Board.new(Denko::Connection::Serial.new)
8
8
  bus = Denko::I2C::Bus.new(board: board, pin: :SDA)
9
- htu21d = Denko::Sensor::HTU21D.new(bus: bus)
9
+ htu21d = Denko::Sensor::HTU21D.new(bus: bus) # address: 0x40 default
10
10
 
11
11
  # Get and set heater state.
12
12
  htu21d.heater_on
@@ -6,26 +6,30 @@ require 'denko'
6
6
 
7
7
  board = Denko::Board.new(Denko::Connection::Serial.new)
8
8
  bus = Denko::I2C::Bus.new(board: board, pin: :SDA)
9
- htu31d = Denko::Sensor::HTU31D.new(bus: bus)
9
+ sensor = Denko::Sensor::HTU31D.new(bus: bus)
10
10
 
11
11
  # Get and set heater state.
12
- htu31d.heater_on
13
- puts "Heater on: #{htu31d.heater_on?}"
14
- htu31d.heater_off
15
- puts "Heater off: #{htu31d.heater_off?}"
12
+ sensor.heater_on
13
+ puts "Heater on: #{sensor.heater_on?}"
14
+ sensor.heater_off
15
+ puts "Heater off: #{sensor.heater_off?}"
16
16
 
17
17
  # Back to default settings, including heater off, unlike HTU21D.
18
- htu31d.reset
19
- puts "Resetting HTU31D... Heater off: #{htu31d.heater_off?}"
18
+ sensor.reset
19
+ puts "Resetting HTU31D..."
20
+ puts "Heater off: #{sensor.heater_off?}"
20
21
  puts
21
22
 
22
23
  # Resolution goes from 0..3 separately for temperature and humidity. See datasheet.
23
- htu31d.temperature_resolution = 3
24
- htu31d.humidity_resolution = 3
24
+ sensor.temperature_resolution = 3
25
+ sensor.humidity_resolution = 3
26
+
27
+ # Get the shared #print_tph_reading method to print readings neatly.
28
+ require_relative 'neat_tph_readings'
25
29
 
26
30
  # Unlike HTU21D, HTU31D works as a regular polled sensor.
27
- htu31d.poll(2) do |reading|
28
- puts "Polled Reading: #{reading[:temperature].round(3)} \xC2\xB0C | #{reading[:humidity].round(3)} % RH"
31
+ sensor.poll(5) do |reading|
32
+ print_tph_reading(reading)
29
33
  end
30
34
 
31
35
  sleep
@@ -0,0 +1,26 @@
1
+ #
2
+ # This helper method can be used in temp/pressure/humidity sensor examples.
3
+ # Give a hash with readings as float values and it prints them neatly.
4
+ #
5
+ def print_tph_reading(reading)
6
+ # Time
7
+ print "#{Time.now.strftime '%Y-%m-%d %H:%M:%S'} - "
8
+
9
+ # Temperature
10
+ formatted_temp = reading[:temperature].to_f.round(2).to_s.ljust(5, '0')
11
+ print "Temperature: #{formatted_temp} \xC2\xB0C"
12
+
13
+ # Pressure
14
+ if reading[:pressure]
15
+ formatted_pressure = (reading[:pressure] / 101325).round(5).to_s.ljust(7, '0')
16
+ print " | Pressure #{formatted_pressure} atm"
17
+ end
18
+
19
+ # Humidity
20
+ if reading[:humidity]
21
+ formatted_humidity = reading[:humidity].round(2).to_s.ljust(5, '0')
22
+ print " | Humidity #{formatted_humidity} %"
23
+ end
24
+
25
+ puts
26
+ end
@@ -0,0 +1,53 @@
1
+ #
2
+ # Example using QMP6988 sensor over I2C, for air temperature and pressure.
3
+ #
4
+ require 'bundler/setup'
5
+ require 'denko'
6
+
7
+ board = Denko::Board.new(Denko::Connection::Serial.new)
8
+ bus = Denko::I2C::Bus.new(board: board, pin: :SDA)
9
+ sensor = Denko::Sensor::QMP6988.new(bus: bus) # address: 0x70 default
10
+
11
+ # Verify chip_id.
12
+ print "I2C device has chip ID: 0x#{sensor.chip_id.to_s(16).upcase}. "
13
+ if sensor.chip_id == 0x5C
14
+ puts "This matches the QMP6988."
15
+ else
16
+ puts "This does not match the QMP6988."
17
+ end
18
+ puts
19
+
20
+ #
21
+ # Change measurement settings:
22
+ # temperature_samples can be 1,2,4,8,16,32 or 64 (default: 1)
23
+ # pressure_samples can be 1,2,4,8,16,32 or 64 (default: 1)
24
+ # iir_coefficient can be 0,2,4,8,16 or 32 (default: 0)
25
+ #
26
+ # High accuracy settings from datasheet, with IIR of 2.
27
+ sensor.temperature_samples = 2
28
+ sensor.pressure_samples = 16
29
+ sensor.iir_coefficient = 2
30
+
31
+ #
32
+ # Change mode (default: forced_mode)
33
+ #
34
+ # Buggy on ESP32S3 in forced mode. Data registers return zeroes on all but first read.
35
+ # Can't recreate on ESP32 V1, AVR or SAMD21. Put it in contiuous mode just in case.
36
+ sensor.continuous_mode
37
+ # sensor.forced_mode
38
+
39
+ #
40
+ # Set standby time (between measurements) for continuous mode only:
41
+ # standby_time (given in ms) can be 1,5,20,250,500,1000,2000 or 4000 (default: 1)
42
+ #
43
+ # sensor.standby_time = 500
44
+
45
+ # Get the shared #print_tph_reading method to print readings neatly.
46
+ require_relative 'neat_tph_readings'
47
+
48
+ # Poll it and print readings.
49
+ sensor.poll(5) do |reading|
50
+ print_tph_reading(reading)
51
+ end
52
+
53
+ sleep
@@ -0,0 +1,15 @@
1
+ #
2
+ # Example using an RCWL-9620 sensor over I2C to measure distance.
3
+ #
4
+ require 'bundler/setup'
5
+ require 'denko'
6
+
7
+ board = Denko::Board.new(Denko::Connection::Serial.new)
8
+ bus = Denko::I2C::Bus.new(board: board, pin: :SDA)
9
+ sensor = Denko::Sensor::RCWL9620.new(bus: bus) # address: 0x57 default
10
+
11
+ sensor.poll(1) do |distance|
12
+ puts "Distance is #{distance} mm"
13
+ end
14
+
15
+ sleep
@@ -0,0 +1,34 @@
1
+ #
2
+ # Example using SHT30/31/35 sensor over I2C, for temperature and humidity.
3
+ #
4
+ require 'bundler/setup'
5
+ require 'denko'
6
+
7
+ board = Denko::Board.new(Denko::Connection::Serial.new)
8
+ bus = Denko::I2C::Bus.new(board: board, pin: :SDA)
9
+ sensor = Denko::Sensor::SHT3X.new(bus: bus) # address: 0x44 default
10
+
11
+ # Heater control
12
+ sensor.heater_on
13
+ puts "Heater on: #{sensor.heater_on?}"
14
+ sensor.heater_off
15
+ puts "Heater off: #{sensor.heater_off?}"
16
+
17
+ # Reset (turns heater off)
18
+ sensor.reset
19
+ puts "Resetting..."
20
+ puts "Heater off: #{sensor.heater_off?}"
21
+ puts
22
+
23
+ # Set repeatability= :low, :medium or :high (default). See datasheet for details.
24
+ sensor.repeatability = :high
25
+
26
+ # Get the shared #print_tph_reading method to print readings neatly.
27
+ require_relative 'neat_tph_readings'
28
+
29
+ # Poll it and print readings.
30
+ sensor.poll(5) do |reading|
31
+ print_tph_reading(reading)
32
+ end
33
+
34
+ sleep
@@ -28,7 +28,7 @@ module Denko
28
28
  # be skipped, since the other 2 calculations depend on it.
29
29
  #
30
30
  # General formula:
31
- # 2 ^ (n-1), where n is the decimal value of the bits, up to 16x max oversampling.
31
+ # 2 ** (n-1), where n is the decimal value of the bits, up to 16x max oversampling.
32
32
  #
33
33
  OVERSAMPLE_FACTORS = {
34
34
  0 => 0b000, # Sensor skipped. Value will be 0x800000.