botcore – basic hardware controls

Core low-level module for CodeBot-CB2.

The botcore module gives you direct access to all the hardware on the CodeBot CB2.
class botcore.Buttons[source]

Access to pushbutton switches BTN-0 and BTN-1.


Return True if specified button is pressed, or tuple of both buttons if num=None.

Parameters:num (int) – 0 for BTN-0, 1 for BTN-1

Return True if specified button was pressed since last call of this function. Default with num=None, returns tuple of both buttons status since last call.

Parameters:num (int) – 0 for BTN-0, 1 for BTN-1
class botcore.CB_LEDs[source]

Manage all the LEDs on CodeBot.


Set both Encoder emitter LEDs (2-bit value).

Parameters:b (int) – Binary value, 0=off, 1=left, 2=right, 3=both

Set all User Line Sensor marker LEDs to the given binary integer value, or list/tuple of bools.

  • bint value used to set LEDs, from 0 to 31 (2 5-1), or
  • blist of 5 bools. (see botcore.CB_LEDs.user for example)
ls_emit(ls_num, pwr)[source]

Set Line Sensor infrared emitter LED to specified power level.

  • ls_num (int) – Number of Line Sensor 0-4
  • pwr (int) – Power level 0-2
ls_num(num, val)[source]

Set single Line Sensor marker LED.

  • num (int) – Number of LED bit, 0-4.
  • val (bool) – Value of LED (True or False)

Set both Proximity marker LEDs.

  • bint binary value, 0=off, 1=left, 2=right, 3=both ; or,
  • blist of 2 bools. (see botcore.CB_LEDs.user for example)
prox_num(num, val)[source]

Set single Proximity marker LED.

  • num (int) – Number of LED bit, 0-1.
  • val (bool) – Value of LED (True or False)

Set Power LED.

Parameters:is_on (bool) – Turn ON if True


Parameters:is_on (bool) – Turn ON if True

Set all User Byte LEDs to the given binary integer value or list/tuple of bools.

  • bint value used to set LEDs, from 0 to 255 (2 8-1) ; or,
  • blist of 8 bools.


leds.user(0)    # turn all User LEDs off
leds.user(255)  # turn all User LEDs on
leds.user(0b10101010)  # Alternating LEDs
leds.user( [True, False, False, True, True, True, True, True] )
user_num(num, val)[source]

Set single User Byte LED.

  • num (int) – Number of LED bit, 0-7.
  • val (bool) – Value of LED (True or False)
class botcore.Encoders(leds)[source]

Manage the wheel encoders. This class demonstrates simple low-level sensing of encoders via ADC channels. Higher performance implementations are available in separate modules, such as timed_encoders.


Check for slot in given encoder disc


Read given analog encoder value

class botcore.Expansion[source]

Access to the Expansion Port

class botcore.LineSensors(leds)[source]

Manage the line sensors.

calibrate(threshold, is_reflective_line)[source]

Set parameters used to detect presence of a line.

  • threshold (int) – Threshold of ADC reading, below which reflection is detected. 0-4095 (2 12-1)
  • is_reflective_line (bool) – Is the line reflective?
check(thresh=1000, is_reflective=False)[source]

Fast check of all line sensors against threshold(s). Controls emitter also.

Return a tuple of values for all line sensors. By default these are bool values indicating presence of a line, based on given parameters. See below for alternate behavior based on parameter types.
  • thresh (int) – Threshold value to compare against sensor readings.
  • is_reflective (bool) – Set to True if the line being checked is reflective (white), False if not (black).

Collection of bool “line detected” values.

  • If thresh=0, returns raw ADC values (ints).
  • If is_reflective is an int value it is interpreted as thresh_upper, and the function returns -1,0,+1 for readings below/within/above thresholds.

Return type:

tuple [5]


Check for presence of line beneath specified Line Sensor using current calibration parameters.

Parameters:num (int) – Number of Line Sensor to read (0-1)

Read the raw ADC value of selected Line Sensor.

Parameters:num (int) – Number of Line Sensor to read (0-1)
class botcore.Motors[source]

Manage the Motors


Enable the motors.

Parameters:do_enable (bool) – Set True to allow motors to run.
run(num, pwr)[source]

Set specified motor to given power level.

  • num (int) – Number of motor: 0 (LEFT) or 1 (RIGHT)
  • pwr (int) – Power -100% to +100% (neg=CW=reverse, pos=CCW=forward)
class botcore.Proximity(leds)[source]

Manage the proximity sensors

detect(power=1, sens=100)[source]

Return bool (L,R) of detection at given sensitivity level.

  • power (int) – Emitter power (1-8).
  • sens (int) – Sensor sensitivity (0-100). Percent range from 0=least to 100=most sensitive.

tuple of (L, R) bool detect status.

range(samples=4, power=1, rng_start=0, rng_stop=100)[source]

Scan proximity sensors and return 0-100% range for each. This function runs a successive approximation algorithm to find the minimum range at which an object is detected by each sensor independently.

  • samples (int) – Number of samples to read. Larger values give more accuracy at the expense of a longer scan time.
  • power (int) – Emitter power (1-8).
  • rng_low (int) – Lowest sensitivity range (0-100) to scan within.
  • rng_high (int) – Highest sensitivity range (0-100) to scan within

0-100 if object detected, or -1 if not. If samples=1, returns bool (L, R) values.

Return type:

tuple of (L, R) int proximity values

class botcore.Speaker[source]

Control the Speaker


Stop output to speaker.

pitch(freq, duty=50)[source]

Play a tone on the speaker, with the specified frequency and duty-cycle.

This function produces a simple Square wave to drive the speaker.

  • freq (int) – Frequency in Hertz
  • duty (int) – Duty-cycle, 0-100% (ratio of high pulse to period of tone)
class botcore.System[source]

General system-level functions


Are we powered by USB or Battery? (based on Power switch)


Measure power supply voltage (battery or USB)


Measure the temperature in Celsius


Measure the temperature in Fahrenheit