ESP32-C3 Bluetooth LE Sensor: Wiring, Code and Simulator
To build this ESP32-C3 Bluetooth LE Sensor project you need an ESP32-C3 DevKitM-1 and a potentiometer. MicroPython bluetooth: scan for nearby (simulated) devices, then advertise an Environmental Sensing service and notify a temperature set by a knob. Make the 3 connections in the wiring table, upload the MicroPython code, and open the Network tab to see the scan, the advertisement and each notification. Turn the knob to change the temperature.
Open this circuit in the free ESP32-C3 DevKitM-1 simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | ESP32-C3 DevKitM-1 | the microcontroller board |
| 1 | Potentiometer | VCC to 5V, GND to GND, SIG to an analog pin. Drag the knob; analogRead gives 0–1023. |
Wiring
| # | From | To |
|---|---|---|
| 1 | ESP32-C3 DevKitM-1 3V3 | Potentiometer VCC |
| 2 | ESP32-C3 DevKitM-1 GND | Potentiometer GND |
| 3 | ESP32-C3 DevKitM-1 IO1 | Potentiometer SIG |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the ESP32-C3 DevKitM-1. No install or account is needed.
- Add the parts. Add a potentiometer from the parts palette.
- Wire 1. Connect ESP32-C3 DevKitM-1 3V3 to Potentiometer VCC.
- Wire 2. Connect ESP32-C3 DevKitM-1 GND to Potentiometer GND.
- Wire 3. Connect ESP32-C3 DevKitM-1 IO1 to Potentiometer SIG.
- Paste the MicroPython code. Paste the MicroPython code below into the code editor.
- Run it. Press Run. Open the Network tab to see the scan, the advertisement and each notification. Turn the knob to change the temperature.
MicroPython code
# ESP32-C3: Bluetooth LE with MicroPython's `bluetooth` module.
# The radio is virtual: a scan finds simulated devices, and what this board
# advertises and notifies shows up in the Network tab. (Connections between
# devices are not simulated.)
import bluetooth, struct, time
from machine import Pin, ADC
ble = bluetooth.BLE()
ble.active(True)
# 1. Scan: the IRQ handler gets one event per device, then "scan done".
seen = {}
def on_event(event, data):
if event == 5: # _IRQ_SCAN_RESULT
addr_type, addr, adv_type, rssi, adv = data
name = None
i = 0
while i + 1 < len(adv): # walk the advertising fields
ln, kind = adv[i], adv[i + 1]
if kind == 0x09:
name = bytes(adv[i + 2:i + 1 + ln]).decode()
i += ln + 1
seen[bytes(addr)] = (name, rssi)
elif event == 6: # _IRQ_SCAN_DONE
print("scan done:", len(seen), "devices")
ble.irq(on_event)
ble.gap_scan(2000)
time.sleep_ms(2100) # results arrive through the IRQ while scanning
for addr, (name, rssi) in seen.items():
print(" %s %-14s %d dBm" % (addr.hex(), name, rssi))
# 2. Be a sensor: an Environmental Sensing service with a temperature value.
ENV = bluetooth.UUID(0x181A)
TEMP = (bluetooth.UUID(0x2A6E), bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY)
((temp_handle,),) = ble.gatts_register_services(((ENV, (TEMP,)),))
name = b"BFB-C3-Sensor"
adv = bytes([2, 0x01, 0x06, len(name) + 1, 0x09]) + name + bytes([3, 0x03, 0x1A, 0x18])
ble.gap_advertise(250_000, adv)
knob = ADC(Pin(1), atten=ADC.ATTN_11DB)
while True:
# Pretend the knob is a thermometer: 0..3.1 V -> 0..40 C, in 0.01 C units.
centi = knob.read_u16() * 4000 // 65535
ble.gatts_write(temp_handle, struct.pack("<h", centi))
ble.gatts_notify(0, temp_handle)
print("temperature %.2f C" % (centi / 100))
time.sleep(2)
What to try
Open the Network tab to see the scan, the advertisement and each notification. Turn the knob to change the temperature.
Frequently asked questions
How do I wire the Bluetooth LE Sensor project on an ESP32-C3 DevKitM-1?
Make 3 connections: ESP32-C3 DevKitM-1 3V3 to Potentiometer VCC; ESP32-C3 DevKitM-1 GND to Potentiometer GND; ESP32-C3 DevKitM-1 IO1 to Potentiometer SIG.
What parts do I need for this ESP32-C3 project?
An ESP32-C3 DevKitM-1 and a potentiometer.
Can I simulate this ESP32-C3 project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and MicroPython code in your browser for free, with no install and no hardware. The ESP32-C3 is emulated instruction by instruction on a RISC-V core running the official MicroPython firmware.
What does the code do?
MicroPython bluetooth: scan for nearby (simulated) devices, then advertise an Environmental Sensing service and notify a temperature set by a knob.
More ESP32-C3 DevKitM-1 projects
- ESP32-C3 Button Interrupt + Blink: Wiring, Code and Simulator
- ESP32-C3 ADC Potentiometer Meter: Wiring, Code and Simulator
- ESP32-C3 PWM, NeoPixel & I2C LCD: Wiring, Code and Simulator
- ESP32-C3 Wi-Fi Weather on an LCD: Wiring, Code and Simulator
- ESP32-C3 SPI MAX7219 LED Matrix: Wiring, Code and Simulator