Skip to main content
SAM Script’s Python mode lets you control SAM Labs hardware using straightforward, readable Python code. You import the built-in sam module, connect to your blocks by name, and call methods to set outputs or read sensor values. If you’ve used Python in class or in another project, the syntax will feel immediately familiar — and the sam module keeps hardware interactions simple and direct.
Python in SAM Script uses snake_case method names (for example, set_color, get_value). This is different from the JavaScript API, which uses camelCase (for example, setColor, getValue). If you switch between languages, keep this naming difference in mind.

Importing and connecting

Start every SAM Script Python program by importing the sam module. Then use sam.connect() to link up with a hardware block:
Store the result in a variable. You’ll use that variable for every subsequent call to that block.
If SAM Studio detects more than one block of the same type nearby, it will ask you to choose which physical block to connect to when sam.connect() runs.

Controlling hardware blocks

LED

Control the LED’s colour and brightness. Pass red, green, and blue values, each between 0 and 255:

Button

Connect to a button and define functions to run each time it is pressed or released:
Pass the function itself — not the result of calling it — to on_press() and on_release(). Notice there are no parentheses after the function names in the last two lines. You can also check the button’s state at any point in a loop:

DC motor

Control the speed and direction of a DC motor:

Servo

Move a servo to a specific angle between 0 and 180 degrees:

Light sensor

Read the current light level or register a callback that fires whenever the value changes:

Buzzer

Play musical notes or raw frequencies on a buzzer block:

Pausing your program

Use sam.wait() to pause execution for a number of milliseconds:

Loops and conditions

Use standard Python while loops and if statements to keep your program running and respond to sensor readings:
Always include a sam.wait() call inside a while True loop. Without it, your program sends commands to the hardware as fast as possible, which can overwhelm the Bluetooth connection. A wait of 50–200 ms is usually a good balance between responsiveness and reliability.

Full example — light-reactive LED

This example reads a light sensor continuously and changes the LED colour depending on how bright or dark the room is:
Try holding your hand over the light sensor to see the LED switch from yellow to blue, then move your hand away to watch it switch back.

Common patterns

Register callback functions that fire automatically when something happens. Your program doesn’t need to check inputs in a loop.Best for: responding to button presses or other discrete events.

JavaScript vs Python: naming conventions

If you use both languages in SAM Script, you’ll notice that the APIs are equivalent but named differently. Python follows PEP 8 conventions and uses snake_case; JavaScript uses camelCase.
Use print() to send messages to the console panel at the bottom of the SAM Script editor. This is the easiest way to inspect sensor readings or check that your program is reaching a certain point.