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

Uploaded Python 3

File details

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

File metadata

  • Download URL: wedo2_python-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 5e5883638fb38f837ade0fc7b1db5ae3fe8c7d3b95111747780e55845269d355
MD5 6d2b477dd59fd49db87da082012450c2
BLAKE2b-256 70d5c99a78061e8508396be975090a336ef2b72f1a7454f8f3cd5423c0bee1f2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wedo2_python-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3b6df1257003dd0838a621bcf1e97e9384dfbe135fb329f171418bc07f279eda
MD5 9754f08e1d8c4af57135d2dc7e919f3a
BLAKE2b-256 b2676b6d2307ff8757d0c7443f885620b3ec2edd3d6134c69940c047513fa292

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