Posts with «iot and connectivity» label

BH1750 Sensor: Measuring Lux with Arduino and the GY-302

The BH1750 sensor converts light intensity into a digital value and makes it available on the I2C bus. Its measurement range goes from 1 to 65,535 lux. On the GY-302 breakout the sensor comes ready to use with Arduino, ESP32 and other boards. AdriRobot tried it with an Arduino UNO and writes about it on his blog.

What the BH1750 sensor measures

The lux is the unit of measurement for illuminance: it corresponds to one lumen per square metre. It therefore describes the light falling on a surface, evaluated as the human eye perceives it. For reference, light reflected by a full Moon is about 1 lx, a bright office is about 400 lx, and sunlight ranges between 32,000 and 100,000 lx.

Measurement modes and resolution

The BH1750 works in six modes, divided into two groups: continuous measurement and single measurement. In continuous mode the sensor detects light without stopping. In single mode it takes one measurement and then shuts down, so it must be reconfigured before the next reading. The library used in the article does this automatically.

Each mode offers three levels of precision:

  • low resolution: 4 lx precision and 16 ms measurement time;
  • high resolution: 1 lx and 120 ms;
  • high resolution 2: 0.5 lx and 120 ms.

By default the library selects continuous high resolution. In low and high resolution modes the maximum value is 54,612.5 lx, while in high resolution 2 it stops at 27,306.25 lx. In practice the sensor returns a 16-bit integer, which the library converts into lux by dividing it by 1.2. A result of -1 means no valid data arrived, while -2 signals an unconfigured device.

You can also change the time interval in which the sensor counts light, through the MTreg register. The value ranges from 32 to 254 and the default is 69.

The GY-302 module: power, pinout and address

The GY-302 breakout with the BH1750 sensor and the pins to be soldered (photo: AdriRobot)

On the GY-302 the sensor is joined by a 3.3 V LDO regulator, marked 662K, and its capacitors. The module therefore accepts Arduino’s 5 V on the VCC pin. The regulator supplies up to 250 mA and withstands a maximum of 6.5 V at its input. On the SCL and SDA lines there are also two 4.7 kΩ pull-up resistors. The breakout is sold as a light intensity sensor.

The connector has five pins: VCC, GND, SCL, SDA and ADDR. The last one is used to choose the I2C address and comes into play when several BH1750 sensors share the same bus. With ADDR at a low level the address is 0x23, with ADDR high it becomes 0x5C. On the module tested the pin is tied to ground through a 4.7 kΩ resistor, so the sensor responds at 0x23.

  • measurement range: from 1 to 65,535 lux;
  • supply voltage: from 3 to 5 V;
  • operating temperature: from -40 to +85 °C;
  • dimensions: 18.5 × 13.9 × 2.5 mm, connectors excluded.

Connecting it to an Arduino UNO and an OLED display

Basic connection between Arduino UNO and GY-302 (diagram: AdriRobot)

For the first test you only need an Arduino UNO board, the GY-302 module and a few male-to-female jumper wires. VCC and GND go to the power supply, while SDA and SCL connect to Arduino’s I2C pins. Before uploading the sketch it is worth running the IDE’s i2c_scanner example: if the wiring is correct, the serial monitor reports a device at address 0x23.

The reading uses Christopher Laws’ BH1750 library, which is installed from the Library Manager together with the Wire library, already included in the IDE. The sketch reads the value every second and also writes it to the serial monitor.

The project is then completed with a 0.96-inch OLED display, 128×64 pixels, SSD1306 driver and I2C interface. The display responds at address 0x3C, so it shares the same bus as the sensor. The Adafruit SSD1306 and Adafruit GFX libraries are needed, and the lux meter shows the lux directly on the screen.

The lux meter with GY-302 and OLED display during the measurement (photo: AdriRobot)

Anyone who wants to see the BH1750 at work in a larger project can read the ESP32 weather station with a live web dashboard, which pairs it with a DHT22 and a BMP280.

Source: the original AdriRobot article also contains the complete schematic, the sketches and the test video.

Related products

The post BH1750 Sensor: Measuring Lux with Arduino and the GY-302 appeared first on Open Electronics.

Upline: a minimal serial protocol for 8-bit IoT

Upline is a minimal serial protocol that turns any microcontroller into an IoT device. The project sheet describes a system based on newline-delimited ASCII records, with a complete Arduino implementation in a single header. The project, by smlcrft, targets 8-bit chips with very limited resources.

The operation is simple. The byte stream is split into lines using LF or CR terminators, ignoring empty lines and those that do not start with the ‘^’ character. Each line is a list of independent entries, delimited by the same ‘^’ character. Each entry represents an operation on a specific key.

How the Upline protocol works

Entries are only processed if fully delimited by two carets. This way partial data is never applied, and an interrupted communication does not leave the device in an inconsistent state. Writes are confirmed via an echo of the value actually in use: this distinguishes a rejection from a loss of communication.

A periodic heartbeat demonstrates that the device is alive and speaks the Upline protocol. This limits the receiver’s wait time, which immediately knows if the node is operational. The protocol also supports escaping special characters with six symbolic sequences, and does not require floating-point numbers: it uses the fixN format for decimals.

  • 4,060 bytes of flash on ATmega328P, 358 bytes of SRAM
  • 3,168 bytes of flash on ATtiny85, 253 bytes of SRAM
  • 2,100 bytes of flash on ATtiny85 in transmit-only mode
  • 98 bytes for a universal line reader on ATmega328P

The numbers are remarkable for a complete protocol. On ATmega328P, 4,060 bytes of flash and 358 bytes of SRAM are needed. On ATtiny85, consumption drops to 3,168 bytes, and in transmit-only mode to 2,100 bytes. The project sheet also reports 98 bytes for a universal line reader on ATmega328P.

Why Upline matters to makers

Upline lets you turn any microcontroller into an IoT device with a minimal, efficient, and robust protocol. The philosophy is the opposite of heavy frameworks: here everything fits in one header and runs even on 8-bit chips with only a few hundred free bytes. An Arduino Nano ESP32 board can use the same protocol as an ATtiny85, simplifying communication between heterogeneous nodes.

The protocol is particularly suited to those building distributed sensors, remote actuators, or small monitoring nodes. Moreover, the echo confirmation makes the system reliable even over noisy radio links. To connect a node to a PC and read the record stream, just use a 3.3 V / 5 V USB-serial converter: Upline speaks over any UART.

Getting started with Upline

To start, simply download the upline-arduino header and include it in an Arduino project. The fact sheet lists support for ATmega328P, ATtiny85, SAMD21, ESP32, and RP2040, among others. An Uno R3 with an ATmega328 is a great test bench to learn the protocol without soldering anything: those are the 4,060 bytes of flash measured by the author.

The source code is a single header file, so integration is immediate. No external libraries or complex build tools are needed. A USB ATtiny85 board is instead the project’s edge case: 3,168 bytes of flash and 253 of SRAM, and the protocol fits entirely inside.

Finally, the project documentation reports precise numbers for each configuration. This helps choose the right microcontroller based on flash and SRAM budget. For the smallest projects, transmit-only mode on ATtiny85 requires just 112 bytes of SRAM.

Source: https://github.com/smlcrft/upline-serial-protocol

Related products

The post Upline: a minimal serial protocol for 8-bit IoT appeared first on Open Electronics.