Skip to main content

A lightweight and modern Python library for controlling the LEGO WeDo 2.0 Smart Hub using Bluetooth Low Energy.

Project description

WeDo2 Python Library

A Python library for controlling the LEGO WeDo 2.0 Smart Hub using Bluetooth Low Energy (BLE).
This project uses the bleak library to communicate with motors, sensors, lights and sound functions on the WeDo 2.0 hub.


Features

  • Connect to the WeDo 2.0 hub by name
  • Control motors (run, stop, brake)
  • Read tilt sensor
  • Read distance sensor
  • Control the LED light (preset colors + RGB)
  • Play notes, frequencies and melodies
  • Read battery level
  • Detect connected devices automatically
  • Turn off or disconnect the hub

Installation

pip install -r requirements.txt

Basic Usage

from wedo2 import WeDo2

hub = WeDo2('WeDo 2.0')

print('Hub name:', hub.name())
print('Battery:', hub.get_battery_level())

hub.disconnect()

Class: WeDo2

Connecting to the hub

hub = WeDo2('WeDo 2.0')

Rename the hub

hub.name('MyHubName')

Read the name of the hub

name = hub.name()
print(name) -> "MyHubName"

Disconnect the hub

hub.disconnect()

Turn off the hub

hub.shut_off()

Read battery level

level = hub.get_battery_level()
print(level)

Motor

Run the motor

hub.motor.run(50, port=1)   # Speed from -100 to 100

Stop the motor

hub.motor.stop(port=1)

Brake the motor

hub.motor.brake(port=1)

Set motor port explicitly

hub.motor.set_port(2)

Distance Sensor

Read distance (0–10 cm)

d = hub.distance_sensor.distance(port=1)
print('Distance:', d)

Read number of triggers

t = hub.distance_sensor.times(port=1)
print('Triggered:', t)

Set sensor port

hub.distance_sensor.set_port(2)

Tilt Sensor

Read tilt direction

position = hub.tilt_sensor.tilt(port=1)
print(position)

Set tilt sensor port

hub.tilt_sensor.set_port(2)

Light

Turn on a preset color

Available colors:
off, pink, purple, blue, sky blue, teal, green, yellow, orange, red, white

hub.light.on('red')

Use integer color

hub.light.on(3)   # blue

RGB color

hub.light.rgb(255, 120, 0)  # orange-ish

Turn off

hub.light.off()

Sound

Play a musical note

Format: C4, D#5, Gb3

hub.sound.note('C4', 500)

Play a beep at frequency

hub.sound.beeb(800, 300)

Play a melody

hub.sound.melody([
    ('C4', 300),
    ('E4', 300),
    ('G4', 500),
    '.',
    ('C5', 800)
])

Raw Commands (Advanced)

Raw output

hub.write_raw_output(b'\x06\x04\x01\x09')

Raw input

hub.write_raw_input(b'\x01\x02\x06\x17\x00\x01\x00\x00\x00\x02\x01')

License

MIT License


Acknowledgements


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wedo2_python-0.1.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wedo2_python-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file wedo2_python-0.1.0.tar.gz.

File metadata

  • Download URL: wedo2_python-0.1.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for wedo2_python-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1d990884c20862931c34c36aa666ae200d411c27b080f858381920d1525681a0
MD5 5a5cc6834bf60a146b9d50aba1ca745a
BLAKE2b-256 9dff91e3755640cbc596929730afe5b04af082bb5913170cc831bca15072be35

See more details on using hashes here.

File details

Details for the file wedo2_python-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: wedo2_python-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for wedo2_python-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2343675df072149b43c7e0b3d6c1dcffc210f84b15ded36d4bcfc27294f918fc
MD5 1f2c9a5ee6d6f4dbcbdb50410a89ac52
BLAKE2b-256 2954d6d1952841c694c73b8967f3132a8911b3b733419485a95df3f41ad84f68

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page