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 wedo2-python

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.3.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.3-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: wedo2_python-0.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 daea2e1cf9dd0eeb34b93f84f817df48d90eff7a1ce656b072f924301c086b77
MD5 208317c95c9afb74e7a67be6b62c6cbd
BLAKE2b-256 8e71d131045c2227637d80c5532f694ebb0f82911ad6c1cef9e76e330e6a7934

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wedo2_python-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.9 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5736cf8bb3f7b74e5d4f51d74780dc37dfec86addd006f90f55727996ab26c8b
MD5 b840f8fe3b8af698bd6d249dcef0d734
BLAKE2b-256 244c388d724781b3abdf69ed04684e4bf95f0cbf18b48bdd183f23a93928ac31

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