04 - Robotbit MicroPython API

 Go back to product documents navigation 

 Go back to Robotbit Documents Navigation 

 

Motor

Run the motor

robotbit.motor(motor number, speed, runtime)

  • motor number: 0 (all motors), 1~4 (corresponds to M1A, M1B, M2A, M2B respectively)
  • speed: -255~255, the negative number means to reverse the motor
  • runtime: stop the motor after a period of time (ms); if set 0, then the motor won't stop.
import robotbit
robotbit.motor(0, 255, 0)
Stop the motor

robotbit.motorstop(motor number)

  • motor number: stop the corresponding motor immediately; 0 (all motors), 1~4 (corresponds to M1A, M1B, M2A, M2B respectively)
import robotbit
robotbit.motorstop(0)

Servo

robotbit.servo(servo number, servo angle)

  • servo number: 0~7 correspond to S1~S8 respectively
  • servo angle: 0~180 correspond to the regular 9g servo (180°)
  • If you're using Kittenbot geekservo (-45°~225°) and 2KG servo (0°~360°), then refer to the code below
import robotbit
robotbit.servo(0, 90)

# geekservo
robotbit.servo(0, int((servo angle-90)/1.5+90))
# 2KG
robotbit.servo(0, int((servo angle-180)*50/9+1500))

Stepper Motor

robotbit.stepper(stepper motor M1 angle, stepper motor M2 angle)

  • the program would not run to the next step if the stepper motor does not turn to the set degree
  • 360 means that the stepper motor turns 360°. The stepper motor here is 28byj.
import robotbit
robotbit.stepper(0, 360)

RGB LED

import microbit
import neopixel

np = neopixel.NeoPixel(microbit.pin16, 4)
# the RGB LEDs on Robotbit are controlled by micro:bit pin16, so here we define the pin as pin16 and we need to use all the 4 LEDs on the board

np[0] = (255, 0, 0)
# Set the first LED color as red. Three parameters correspond red, green, and blue.

np.show()
# show the color you set. If not adding this line, the LED won't show the color.

Buzzer

We use the official mocro:bit library here. The default is P0.
music.pitch(frequency, duration)
frequency-pitch reference

import music
music.pitch(262, 500)
# this is the DO in the middle and lasts for 500ms

 

music.play(note)

  • note = NOTE[octave][:duration]

import music
music.play('c4:4')
# c4:1 correspond to octave:duration. There are two points that affects the duration: ticks and bpm.
# default duration = 600000/bpm/ticks, default bpm = 120, tick = 4, so c4:1 means that the DO in the fourth octave sounds for 500 ms
# If music.play('c') has the same effect as c4:4, it is because the default octave = 4, duration = 4
# there are also some built-in melodies in the music library, like music.play(music.FUNK)

 

Built-in melodies:

DADADADUM、ENTERTAINER、PRELUDE、ODE、NYAN、RINGTONE、FUNK、BLUES、BIRTHDAY、WEDDING、FUNERAL、PUNCHLINE、PYTHON、BADDY、CHASE、BA_DING、WAWAWAWAA、JUMP_UP、JUMP_DOWN、POWER_UP、POWER_DOWN

 

Run the official sample program and enjoy the melody:

from microbit import *
import music

# play Prelude in C.
notes = [
        'c4:1', 'e', 'g', 'c5', 'e5', 'g4', 'c5', 'e5', 'c4', 'e', 'g', 'c5', 'e5', 'g4', 'c5', 'e5',
        'c4', 'd', 'a', 'd5', 'f5', 'a4', 'd5', 'f5', 'c4', 'd', 'a', 'd5', 'f5', 'a4', 'd5', 'f5',
        'b3', 'd4', 'g', 'd5', 'f5', 'g4', 'd5', 'f5', 'b3', 'd4', 'g', 'd5', 'f5', 'g4', 'd5', 'f5',
        'c4', 'e', 'g', 'c5', 'e5', 'g4', 'c5', 'e5', 'c4', 'e', 'g', 'c5', 'e5', 'g4', 'c5', 'e5',
        'c4', 'e', 'a', 'e5', 'a5', 'a4', 'e5', 'a5', 'c4', 'e', 'a', 'e5', 'a5', 'a4', 'e5', 'a5',
        'c4', 'd', 'f#', 'a', 'd5', 'f#4', 'a', 'd5', 'c4', 'd', 'f#', 'a', 'd5', 'f#4', 'a', 'd5',
        'b3', 'd4', 'g', 'd5', 'g5', 'g4', 'd5', 'g5', 'b3', 'd4', 'g', 'd5', 'g5', 'g4', 'd5', 'g5',
        'b3', 'c4', 'e', 'g', 'c5', 'e4', 'g', 'c5', 'b3', 'c4', 'e', 'g', 'c5', 'e4', 'g', 'c5',
        'a3', 'c4', 'e', 'g', 'c5', 'e4', 'g', 'c5', 'a3', 'c4', 'e', 'g', 'c5', 'e4', 'g', 'c5',
        'd3', 'a', 'd4', 'f#', 'c5', 'd4', 'f#', 'c5', 'd3', 'a', 'd4', 'f#', 'c5', 'd4', 'f#', 'c5',
        'g3', 'b', 'd4', 'g', 'b', 'd', 'g', 'b', 'g3', 'b3', 'd4', 'g', 'b', 'd', 'g', 'b'
]

music.play(notes)

Kittenbot Ultrasonic Sensor

It is an ultrasonic sensor with 2 RGB LEDs. For regular HC-04 ultrasonic sensor, please short-circuit the TRIG and ECHO

robotbit.sonar(pin)

import microbit
import robotbit
robotbit.sonar(microbit.pin1)

 

If you have any question, please feel free to contact us at Discord, we will always be there to help.

 

KittenBot Team

 

 Go back to product documents navigation 

 Go back to Robotbit Documents Navigation 

Leave a comment

Please note, comments need to be approved before they are published.

SUBHEADING

Blog posts