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

Uploaded Python 3

File details

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

File metadata

  • Download URL: wedo2_python-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 b10ab6d939a5d64dadda894868f36a55aa6af5d43bfec53f5e42cc377d23b6bb
MD5 8e704775c227fcd0f5807d65998f343f
BLAKE2b-256 1537f1bcc1df75c41c44ea86c3fda0fcc9205ae6d45ff960df038dd11fbb9282

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wedo2_python-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 53e4adfff3512b797804d7bcadaeecbfe47c6f133486377e7dd91911227d4719
MD5 0039afa6f35416afcccbbaf648ab71a1
BLAKE2b-256 44389053e6c969009dce8f572b6ddc0ee9e3d4c032ecf79ea08c466dae9c6c6a

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