module modules

class Ultrasonic

Example

# Import Ultrasonic and Pin class
from robot_hat import Ultrasonic, Pin

# Create Motor object
us = Ultrasonic(Pin("D2"), Pin("D3"))

# Read distance
distance = us.read()
print(f"Distance: {distance}cm")

API

class robot_hat.modules.Ultrasonic(trig, echo, timeout=0.02)
__init__(trig, echo, timeout=0.02)

class ADXL345

Example

# Import ADXL345 class
from robot_hat import ADXL345

# Create ADXL345 object
adxl = ADXL345()
# or with a custom I2C address
adxl = ADXL345(address=0x53)

# Read acceleration of each axis
x = adxl.read(adxl.X)
y = adxl.read(adxl.Y)
z = adxl.read(adxl.Z)
print(f"Acceleration: {x}, {y}, {z}")

# Or read all axis at once
x, y, z = adxl.read()
print(f"Acceleration: {x}, {y}, {z}")
# Or print all axis at once
print(f"Acceleration: {adxl.read()}")

API

class robot_hat.ADXL345(*args, address: int = 83, bus: int = 1, **kwargs)

Bases: I2C

ADXL345 modules

X = 0

X

Y = 1

Y

Z = 2

Z

__init__(*args, address: int = 83, bus: int = 1, **kwargs)

Initialize ADXL345

Parameters

address (int) – address of the ADXL345

read(axis: int = None) Union[float, List[float]]

Read an axis from ADXL345

Parameters

axis (int) – read value(g) of an axis, ADXL345.X, ADXL345.Y or ADXL345.Z, None for all axis

Returns

value of the axis, or list of all axis

Return type

float/list

class RGB_LED

Example

# Import RGB_LED and PWM class
from robot_hat import RGB_LED, PWM

# Create RGB_LED object for common anode RGB LED
rgb = RGB_LED(PWM(0), PWM(1), PWM(2), common=RGB_LED.ANODE)
# or for common cathode RGB LED
rgb = RGB_LED(PWM(0), PWM(1), PWM(2), common=RGB_LED.CATHODE)

# Set color with 24 bit int
rgb.color(0xFF0000) # Red
# Set color with RGB tuple
rgb.color((0, 255, 0)) # Green
# Set color with RGB List
rgb.color([0, 0, 255]) # Blue
# Set color with RGB hex string starts with “#”
rgb.color("#FFFF00") # Yellow

API

class robot_hat.RGB_LED(r_pin: PWM, g_pin: PWM, b_pin: PWM, common: int = 1)

Simple 3 pin RGB LED

ANODE = 1

Common anode

CATHODE = 0

Common cathode

__init__(r_pin: PWM, g_pin: PWM, b_pin: PWM, common: int = 1)

Initialize RGB LED

Parameters
  • r_pin (robot_hat.PWM) – PWM object for red

  • g_pin (robot_hat.PWM) – PWM object for green

  • b_pin (robot_hat.PWM) – PWM object for blue

  • common (int) – RGB_LED.ANODE or RGB_LED.CATHODE, default is ANODE

Raises
  • ValueError – if common is not ANODE or CATHODE

  • TypeError – if r_pin, g_pin or b_pin is not PWM object

color(color: Union[str, Tuple[int, int, int], List[int], int])

Write color to RGB LED

Parameters

color (str/int/tuple/list) – color to write, hex string starts with “#”, 24-bit int or tuple of (red, green, blue)

class Buzzer

Example

Imports and initialize

# Import Buzzer class
from robot_hat import Buzzer
# Import Pin for active buzzer
from robot_hat import Pin
# Import PWM for passive buzzer
from robot_hat import PWM
# import Music class for tones
from robot_hat import Music
# Import time for sleep
import time

music = Music()
# Create Buzzer object for passive buzzer
p_buzzer = Buzzer(PWM(0))
# Create Buzzer object for active buzzer
a_buzzer = Buzzer(Pin("D0"))

Active buzzer beeping

while True:
    a_buzzer.on()
    time.sleep(0.5)
    a_buzzer.off()
    time.sleep(0.5)

Passive buzzer Simple usage

# Play a Tone for 1 second
p_buzzer.play(music.note("C3"), duration=1)
# take adventage of the music beat as duration
# set song tempo of the beat value
music.tempo(120, 1/4)
# Play note with a quarter beat
p_buzzer.play(music.note("C3"), music.beat(1/4))

Passive buzzer Manual control

# Play a tone
p_buzzer.play(music.note("C4"))
# Pause for 1 second
time.sleep(1)
# Play another tone
p_buzzer.play(music.note("C5"))
# Pause for 1 second
time.sleep(1)
# Stop playing
p_buzzer.off()

Play a song! Baby shark!

music.tempo(120, 1/4)

# Make a Shark-doo-doo function as is all about it
def shark_doo_doo():
    p_buzzer.play(music.note("C5"), music.beat(1/8))
    p_buzzer.play(music.note("C5"), music.beat(1/8))
    p_buzzer.play(music.note("C5"), music.beat(1/8))
    p_buzzer.play(music.note("C5"), music.beat(1/16))
    p_buzzer.play(music.note("C5"), music.beat(1/16 + 1/16))
    p_buzzer.play(music.note("C5"), music.beat(1/16))
    p_buzzer.play(music.note("C5"), music.beat(1/8))

# loop any times you want from baby to maybe great great great grandpa!
for _ in range(3):
    print("Measure 1")
    p_buzzer.play(music.note("G4"), music.beat(1/4))
    p_buzzer.play(music.note("A4"), music.beat(1/4))
    print("Measure 2")
    shark_doo_doo()
    p_buzzer.play(music.note("G4"), music.beat(1/8))
    p_buzzer.play(music.note("A4"), music.beat(1/8))
    print("Measure 3")
    shark_doo_doo()
    p_buzzer.play(music.note("G4"), music.beat(1/8))
    p_buzzer.play(music.note("A4"), music.beat(1/8))
    print("Measure 4")
    shark_doo_doo()
    p_buzzer.play(music.note("C5"), music.beat(1/8))
    p_buzzer.play(music.note("C5"), music.beat(1/8))
    print("Measure 5")
    p_buzzer.play(music.note("B4"), music.beat(1/4))
    time.sleep(music.beat(1/4))

API

class robot_hat.Buzzer(buzzer: Union[PWM, Pin])
__init__(buzzer: Union[PWM, Pin])

Initialize buzzer

Parameters

pwm (robot_hat.PWM/robot_hat.Pin) – PWM object for passive buzzer or Pin object for active buzzer

on()

Turn on buzzer

off()

Turn off buzzer

freq(freq: float)

Set frequency of passive buzzer

Parameters

freq (int/float) – frequency of buzzer, use Music.NOTES to get frequency of note

Raises

TypeError – if set to active buzzer

play(freq: float, duration: float = None)

Play freq

Parameters
  • freq (float) – freq to play, you can use Music.note() to get frequency of note

  • duration (float) – duration of each note, in seconds, None means play continuously

Raises

TypeError – if set to active buzzer

class Grayscale_Module

Example

# Import Grayscale_Module and ADC class
from robot_hat import Grayscale_Module, ADC

# Create Grayscale_Module object, reference should be calculate from the value reads on white
# and black ground, then take the middle as reference
gs = Grayscale_Module(ADC(0), ADC(1), ADC(2), reference=2000)

# Read Grayscale_Module datas
datas = gs.read()
print(f"Grayscale Module datas: {datas}")
# or read a specific channel
l = gs.read(gs.LEFT)
m = gs.read(gs.MIDDLE)
r = gs.read(gs.RIGHT)
print(f"Grayscale Module left channel: {l}")
print(f"Grayscale Module middle channel: {m}")
print(f"Grayscale Module right channel: {r}")

# Read Grayscale_Module simple states
state = gs.read_status()
print(f"Grayscale_Module state: {state}")

API

class robot_hat.Grayscale_Module(pin0: ADC, pin1: ADC, pin2: ADC, reference: int = None)

3 channel Grayscale Module

LEFT = 0

Left Channel

MIDDLE = 1

Middle Channel

RIGHT = 2

Right Channel

__init__(pin0: ADC, pin1: ADC, pin2: ADC, reference: int = None)

Initialize Grayscale Module

Parameters
  • pin0 (robot_hat.ADC/int) – ADC object or int for channel 0

  • pin1 (robot_hat.ADC/int) – ADC object or int for channel 1

  • pin2 (robot_hat.ADC/int) – ADC object or int for channel 2

  • reference (1*3 list, [int, int, int]) – reference voltage

reference(ref: list = None) list

Get Set reference value

Parameters

ref (list) – reference value, None to get reference value

Returns

reference value

Return type

list

read_status(datas: list = None) list

Read line status

Parameters

datas (list) – list of grayscale datas, if None, read from sensor

Returns

list of line status, 0 for white, 1 for black

Return type

list

read(channel: int = None) list

read a channel or all datas

Parameters

channel (int/None) – channel to read, leave empty to read all. 0, 1, 2 or Grayscale_Module.LEFT, Grayscale_Module.CENTER, Grayscale_Module.RIGHT

Returns

list of grayscale data

Return type

list