عرض الفئات

Arduino Extension – Physical Computing in Scratch

🔌 Arduino Extension – Physical Computing in Scratch #

The Arduino extension brings real-world electronics and robotics into Scratch.
Control LEDs, motors, servos, sensors, and build interactive physical projects with simple blocks.
Perfect for students, makers, and STEM education. ✨


🌟 Overview #

  • Physical Computing: Control real hardware – LEDs, motors, servos, sensors, and more.
  • Arduino Support: Works with Arduino Uno, Mega, Nano, Leonardo, and compatible boards.
  • Connection: Uses browser-based serial and bluetooth BLE communication. For full connection steps, see:
    How Pishi.ai Scratch Extensions Connect to Hardware.
  • Standard Firmata: Arduino communicates with Scratch using the Firmata protocol.
  • Rich Blocks: 40+ blocks for digital/analog I/O, PWM, servos, RGB LEDs, motors, I2C, and more.
  • Real-Time Control: Instant feedback – see hardware respond as your code runs.

✨ Key Features #

  • Digital I/O: Read buttons, control LEDs, detect signals.
  • Analog Input: Read sensors (light, temperature, distance, potentiometers, etc.).
  • PWM Output: Control LED brightness, motor speed, RGB colors.
  • Servo Control: Position servos, create robotic movements.
  • RGB LED Control: 13 predefined colors, custom RGB values, fades, and blinks.
  • Motor Control: DC motor speed and direction with H-bridge support.
  • Sound/Beep: 13 predefined sound patterns (beeps, alerts, alarms, SOS).
  • I2C Communication: Advanced sensor/device communication.
  • Smart Pin Reading: Auto-detects pin modes for easy sensor reading.
  • Advanced Blocks: Edge detection, threshold monitoring, value mapping.

🚀 How to Use #

Step 1 – Install StandardFirmata #

Arduino must have StandardFirmata installed.

✔ Follow this guide:
كيفية تثبيت Firmata القياسية على Arduino


Step 2 – Connect Arduino to Scratch #

1. Open: pishi.ai/play
2. Add the Arduino extension
3. Click the Connect button
4. Choose your Arduino’s port

For full connection instructions, permissions, browser support, and macOS-specific steps, see:


🧱 Blocks and Functions #

 

⚡ Core I/O – Digital Pins #

write digital pin [PIN] [0/1]

Sets a digital pin to HIGH (1) or LOW (0).
Use this to turn LEDs on/off, control relays, send digital signals.

Arguments:

  • [PIN] – Digital pin number (e.g., 2, 3, 13)
  • [0/1] – 0 = LOW (off), 1 = HIGH (on)

Example: write digital pin [13] [1] → Turns on Arduino’s built-in LED

 

read digital pin [PIN]

Reads the current state of a digital pin – returns 0 (LOW) or 1 (HIGH).
Use this to read buttons, switches, or digital sensors.

Returns: 0 or 1

Example: read digital pin [2] → Returns 0 (LOW) or 1 (HIGH) from pin 2


📊 Core I/O – Analog and PWM #

write pwm pin [PIN] [VALUE]%

Writes a PWM (Pulse Width Modulation) value to a pin – controls brightness, speed, or analog-like output.
PWM pins are marked with ~ on Arduino boards (e.g., pins 3, 5, 6, 9, 10, 11 on Uno).

Arguments:

  • [PIN] – PWM-capable pin number
  • [VALUE] – Percentage (0–100): 0 = off, 100 = full power

Example: write pwm pin [9] [50]% → Sets LED to 50% brightness

 

read analog pin [PIN]

Reads an analog value from an analog pin (a0–a5 on Uno, a0–a15 on Mega).
Returns a value from 0 (0V) to 1023 (5V).

Returns: 0–1023

Example: Read a potentiometer value, light sensor brightness, or temperature sensor voltage.

 

read pin [PIN] (auto detect mode)

Smart pin reading – automatically detects if the pin should be read as digital or analog.
Useful when you’re not sure of the pin mode or want flexible sensor reading.

Returns: Pin value (digital 0/1 or analog 0–1023)

 


💡 LED Control #

turn on led on pin [PIN]
clear led on pin [PIN]

Simple LED control – turn LEDs on or off.
Convenience blocks for common LED operations.

Example:

  • turn on led on pin [13] → Lights up built-in LED
  • clear led on pin [13] → Turns it off

 

blink led on pin [PIN] [TIMES] times, on: [ON_TIME] off: [OFF_TIME] ms

Advanced LED blink with separate on/off durations.
Create custom blink patterns – fast, slow, morse code, etc.

Arguments:

  • [PIN] – LED pin number
  • [TIMES] – Number of blinks
  • [ON_TIME] – LED on duration (milliseconds)
  • [OFF_TIME] – LED off duration (milliseconds)

Example: blink led on pin [13] [3] times, on: [200] off: [500] ms → Blinks 3 times slowly

 


🌈 RGB LED Control #

set rgb led r:[RPIN] g:[GPIN] b:[BPIN] to r:[RED] g:[GREEN] b:[BLUE]

Control RGB LEDs with custom color values (0–255).
Mix red, green, and blue to create any color.

Arguments:

  • [RPIN/GPIN/BPIN] – PWM pins for red, green, blue channels
  • [RED/GREEN/BLUE] – Color intensity (0–255): 0 = off, 255 = brightest

Example: set rgb led r:[9] g:[10] b:[11] to r:[255] g:[0] b:[0] → Pure red

 

set rgb led r:[RPIN] g:[GPIN] b:[BPIN] to [COLOR]

Set RGB LED to predefined colors – easy color selection from a menu.

Available Colors:

  • off, red, green, blue, white, yellow, cyan, magenta, orange, purple, pink, lime, indigo

Example: set rgb led r:[9] g:[10] b:[11] to [yellow]

 

blink rgb led r:[RPIN] g:[GPIN] b:[BPIN] [COLOR1]↔[COLOR2] [TIMES]x[DELAY] ms

Blink RGB LED between two colors – create colorful patterns.

Example: blink rgb led r:[9] g:[10] b:[11] [red]↔[blue] [5]x[300] ms → Blinks between red and blue 5 times

 

fade in rgb led r:[RPIN] g:[GPIN] b:[BPIN] to [COLOR] over [DURATION] ms
fade out rgb led r:[RPIN] g:[GPIN] b:[BPIN] from [COLOR] over [DURATION] ms

Smooth RGB LED fades – gradually fade in from off to a color, or fade out from a color to off.
Creates smooth, professional lighting effects.

Example: fade in r:[9] g:[10] b:[11] to [white] over [1000] ms → Slowly brightens to white over 1 second

 


🔊 Sound/Beep Control #

beep on pin [PIN]

Simple beep sound – short tone on specified pin.
Connect a buzzer or piezo speaker to the pin.

 

beep on pin [PIN] for [DURATION] ms

Beep with custom duration – control beep length precisely.

Example: beep on pin [8] for [500] ms → Half-second beep

 

play [PATTERN] pattern on pin [PIN]

Play predefined beep patterns – built-in sound effects for alerts, notifications, etc.

Available Patterns:

  • short beep – Quick single beep
  • long beep – Extended single beep
  • double beep – Two quick beeps
  • triple beep – Three quick beeps
  • success – Short + long beep (achievement sound)
  • victory – Quick celebratory sequence
  • error – Irregular error tone
  • warning – Multi-beep alert
  • failure – Two long sad beeps
  • alarm – Three urgent long beeps
  • notification – Friendly notification sound
  • heartbeat – Rhythmic beep-pause-beep
  • sos – Morse code SOS distress signal

Example: play [victory] pattern on pin [8]

 

stop beep on pin [PIN]
stop beep on pin [PIN] for [DURATION] ms

Stop beep output – silence the buzzer immediately or for a specified duration.

 


🤖 Servo Control #

set servo on pin [PIN] to angle: [ANGLE]

Control servo motor position – set angle from 0° to 180°.
Perfect for robotic arms, pan-tilt mechanisms, animatronics.

Arguments:

  • [PIN] – Servo signal pin (usually PWM-capable)
  • [ANGLE] – Position in degrees (0–180)

Example: set servo on pin [9] to angle: [90] → Centers servo

 

move servo on pin [PIN] from: [ANGLE1] to: [ANGLE2] speed: [SPEED]

Smooth servo movement – move gradually from one angle to another at controlled speed.
Creates natural, smooth robotic movements.

Arguments:

  • [SPEED] – Movement speed (1–100): 1 = slowest, 100 = fastest

Example: move servo [2] from: [0] to: [180] speed: [50]

 

stop servo on pin [PIN]

Stop servo control – releases servo signal (servo relaxes).

 


⚙️ Motor Control #

set motor on pins [PIN1] [PIN2] speed: [SPEED] direction: [DIRECTION]

Control DC motors with H-bridge – set speed and direction.
Common H-bridges: L298N, L293D.

Arguments:

  • [PIN1/PIN2] – H-bridge control pins
  • [SPEED] – Motor speed (0–100%)
  • [DIRECTION] – forward, backward, or stop

Example: set motor on pins [9] [10] speed: [75] direction: [forward]

 

stop motor on pins [PIN1] [PIN2]

Stop DC motor – sets both pins to 0 (motor brake).

 


🎯 Advanced Sensor Blocks #

wait until digital pin [PIN] becomes [0/1]

Wait for a digital pin state change – blocks script until condition is met.
Perfect for button presses, sensor triggers.

Example: wait until digital pin [2] becomes [1] → Waits for button press

 

is digital pin [PIN] [0/1]?

Boolean sensor check – returns true if pin matches specified state.

Example: is digital pin [2] [1] → Returns true if pin 2 is reading a value of 1.

 

pin [PIN] [EDGE_TYPE] edge detected?

Detect signal edges – rising edge (0→1), falling edge (1→0), or any change.
Advanced input detection for events, counters, encoders.

Edge Types:

  • rising – Detected when pin goes from LOW to HIGH
  • falling – Detected when pin goes from HIGH to LOW
  • change – Detected on any state change

Example: pin [2] [rising] edge detected? → Returns true when pin 2 switches from 0 to 1.

 

is analog pin [PIN] between [MIN] and [MAX]?

Analog range check – returns true if sensor value is within range.

Example: is analog pin [a0] between [300] and [700]? → Returns true when the analog reading on a0 falls within 300–700

 

wait until analog pin [PIN] [CONDITION] [THRESHOLD]

Wait for analog threshold – blocks until sensor meets condition (above/below/equals value).

Conditions: above, below, equals

Example: wait until analog pin [a0] [above] [500] → Waits until sensor > 500

 

analog pin [PIN] changed by more than [SENSITIVITY]?

Detect analog value changes – returns true if sensor changed significantly.
Useful for detecting motion, touch, or environmental changes.

Example: analog pin [a0] changed by more than [50]→ Detects a big change (more than 50) in the sensor value on a0.

 


🔧 Configuration Blocks #

set pin [PIN] mode to [MODE]

Configure pin mode – input, output, input_pullup, pwm, servo, analog.
Usually automatic, but useful for advanced control.

Modes:

  • digital input – Digital 0/1 input (buttons, switches)
  • digital input: pullup – Digital input with internal pull-up resistor
  • output: 0 – Digital output in LOW state
  • output: 1 – Digital output in HIGH state
  • pwm – PWM output
  • servo – RC Servo output
  • analog input – Analog input (0-1023)

 

pin [PIN] mode

Get current pin mode – returns mode name as text.

 

pin [PIN] state

Get current pin state value – returns current pin value.

 


🔌 I2C Communication (Advanced) #

enable i2c with [DELAY] µs delay

Enable I2C bus – required before using I2C sensors/devices.
I2C uses pins a4 (SDA) and a5 (SCL) on Uno.

 

i2c write to address [ADDRESS] data [DATA]

Write data to I2C device – send commands or values to sensors/displays.

Example: i2c write to address [0x48] data [0x00]

 

i2c read [BYTES] byte(s) from [ADDRESS] register [REGISTER] as [FORMAT]

Read data from I2C device – get sensor readings or device status.

Formats: hex, decimal (unsigned), decimal (signed), float, text (ASCII)

Example: i2c read [2] byte(s) from [0x48] register [0x00] as [decimal: unsigned]

 

disable i2c and release pins

Disable I2C – frees up I2C pins for other uses.

 

i2c enabled?

Check I2C status – returns true if I2C is currently enabled.

 


📡 Serial/BLE Communication (Advanced) #

enable serial/ble on rx: [RX_PIN] tx: [TX_PIN]

Enables Serial or BLE communication and protects the selected pins.
This is used when connecting external BLE modules to Scratch on Arduino boards that do not have built-in Bluetooth.
Works with StandardFirmataPishiAi or other modified StandardFirmata firmware.

Once enabled, the chosen pins are locked so other Scratch blocks cannot change them by mistake.

Arguments

  • [RX_PIN] – Receive pin for serial communication
  • [TX_PIN] – Transmit pin for serial communication

Primary Use

  • External BLE Modules: Protects the serial pins used for serial or BLE modules like HC-05, HC-06, and HM-10 so Scratch can connect through StandardFirmataPishiAi firmware.

Note: These blocks can also be used simply to protect and reserve two important pins from accidental changes in your project.

Example:
enable serial/ble on rx: [4] tx: [7]

 

disable serial/ble and release pins

Disables Serial or BLE communication and frees the protected pins.
Use this when you no longer need the BLE connection and want to reuse the pins for sensors or outputs.

 

serial/ble enabled?

Checks if Serial or BLE protection is active.
Returns true when enabled and false when disabled.

 


📐 Utility Blocks #

map [VALUE] from [FROM_LOW]-[FROM_HIGH] to [TO_LOW]-[TO_HIGH]

Map value from one range to another – essential for sensor calibration.
Similar to Arduino’s map() function.

Example: map [512] from [0]-[1023] to [0]-[100] → Returns 50.05 (converts analog reading to percentage)

Common Uses:

  • Convert analog sensor (0–1023) to percentage (0–100)
  • Scale servo angles or motor speeds
  • Calibrate sensor ranges

 

firmata version

Get Firmata version – returns version string (e.g., “2.5”).
Useful for debugging connection issues.

 


🎓 Educational Uses #

  • Physical Computing – Students learn how software controls hardware – bridging digital and physical worlds.
  • Teach electronics basics – circuits, voltage, current, digital vs analog, PWM.
  • STEM/STEAM projects – robotics, automation, IoT, environmental monitoring.
  • Sensor-based interactivity – create projects that respond to light, sound, motion, temperature, distance.
  • Robotics education – build and program robots with servos, motors, sensors.
  • Maker culture – hands-on learning, prototyping, creative problem-solving.
  • Cross-curricular projects – combine coding, engineering, art, science.

🎮 Example Projects #

  • Traffic Light System: Control RGB LED or 3 LEDs with timed sequences.
  • Obstacle Avoider Robot: Use distance sensor + motors to navigate.
  • Smart Home Controller: Control lights, fans, alarms with sensors and buttons.
  • Weather Station: Read temperature/humidity sensors, display on Scratch stage.
  • Interactive Game Controller: Use buttons, joysticks, potentiometers as game inputs.
  • Plant Watering System: Monitor soil moisture, activate pump when dry.
  • Robotic Arm: Control multiple servos to pick and place objects.
  • LED Music Visualizer: Sync RGB LED colors/brightness to Scratch music.
  • Line Following Robot: Use IR sensors + motors for autonomous navigation.
  • Arduino Piano: Read analog sensors or buttons, play corresponding notes in Scratch.

🧩 Try it yourself: pishi.ai/play

 


🔧 Tips & Troubleshooting #

 

🔌 Arduino Specific Tips #

  • Connection fails? Make sure StandardFirmata is installed for USB, and StandardFirmataBLE (or compatible firmware) is installed for Bluetooth.
  • USB port not appearing? Close Arduino IDE Serial Monitor – it blocks the port. Try unplugging/replugging USB.
  • Browser compatibility for USB? Use Chrome, Edge, Opera, or other Chromium browsers on desktop. Safari and Firefox may not work with Web USB.
  • Browser compatibility for BLE? Web Bluetooth is supported in Chrome and Edge on desktop. Safari and Firefox usually do not support BLE.
  • Pin not responding? Check wiring, ensure correct pin number, verify pin supports the operation (PWM, analog, etc.).
  • LED not turning on? Add a resistor (220Ω–1kΩ) to protect the LED. Check polarity (usually long leg = positive).
  • Servo jittering? Use external power for servos – Arduino USB power is often insufficient.
  • Analog readings noisy? Add a small capacitor (0.1µF) across sensor pins for stability.
  • Motor not working? DC motors need external power and H-bridge driver – never connect directly to Arduino pins.
  • Firmata version mismatch? Upload the latest StandardFirmata from your Arduino IDE version.
  • Multiple Arduinos? Each Arduino needs its own connection – disconnect/reconnect to switch boards.
  • RGB LED wrong colors? Check whether your LED is common anode (colors need inverted logic) or common cathode (normal logic).
    Also, some RGB LEDs need different resistor values for each color to match brightness correctly.
  • I2C not working? Use pull-up resistors (4.7kΩ) on SDA/SCL lines. Check device address with I2C scanner sketch.

⚡ Pin Reference #

– Arduino Uno / Nano (Classic) #

  • Digital Pins: 0–13 (avoid 0 and 1 if using serial)
  • PWM Pins: 3, 5, 6, 9, 10, 11
  • Analog Inputs: a0–a5 (also usable as digital 14–19)
  • I2C: a4 (SDA), a5 (SCL)
  • Built-in LED: pin 13

– Arduino Mega 2560 #

  • Digital Pins: 0–53
  • PWM Pins: 2–13, 44–46
  • Analog Inputs: a0–a15
  • I2C: 20 (SDA), 21 (SCL)

– Arduino Micro / Leonardo #

  • Digital Pins: 0–13 + extra 14–21
  • PWM Pins: 3, 5, 6, 9, 10, 11, 13
  • Analog Inputs: a0–a5 (plus a6–a11 depending on revision)
  • I2C: 2 (SDA), 3 (SCL)
  • Built-in LED: pin 13

– Arduino Nano 33 BLE / BLE Sense #

  • Digital Pins: 0–13
  • PWM Pins: 3, 5, 6, 9, 10, 11, 12
  • Analog Inputs: a0–a7
  • I2C: a4 (SDA), a5 (SCL)
  • Notes: Runs at 3.3V only.

– Arduino MKR WiFi 1010 #

  • Digital Pins: 0–13
  • PWM Pins: 0–8
  • Analog Inputs: a0–a6
  • I2C: 11 (SDA), 12 (SCL)
  • Notes: 3.3V logic, BLE and WiFi support.

– Arduino Uno WiFi Rev2 #

  • Digital Pins: 0–13
  • PWM Pins: 3, 5, 6, 9, 10, 11
  • Analog Inputs: a0–a5
  • I2C: SDA/SCL on dedicated pins
  • Built-in LED: pin 13

Note: The boards listed above are the most common ones. Other Arduino-compatible boards may also work with this extension as long as they support StandardFirmata or StandardFirmataBLE.

 


🔒 Safety Notes #

  • Current Limits: Arduino pins provide max 40mA – never exceed or damage occurs. Use transistors/relays for high-power loads.
  • Voltage Limits: Arduino operates at 5V (Uno/Mega) or 3.3V (some boards). Never connect higher voltages directly.
  • LED Resistors: Always use current-limiting resistors (220Ω–1kΩ) with LEDs to prevent burnout.
  • Motor Power: DC motors, servos, and high-power devices need external power supplies – shared ground with Arduino.
  • Reverse Polarity: Check polarity before connecting power – reversed voltage can destroy components.
  • Short Circuits: Avoid wiring mistakes that create short circuits – can damage Arduino or computer USB port.
  • Heat Dissipation: Don’t run high-current loads continuously – components can overheat.

🧪 Technical Info #

  • Protocol: StandardFirmata and its variants (such as StandardFirmataBLE), based on the Firmata protocol.
  • Connection: USB via Browser or BLE via Browser (browser-native, no drivers needed)
  • Supported Boards: Arduino Uno, Mega, Nano, Leonardo, and compatible boards
  • Baud Rate: 57600 (standard Firmata default)
  • Minimum StandardFirmata Version: 2.0
  • Digital Pins: 0/1 values (LOW/HIGH)
  • Analog Range: 0–1023 (10-bit ADC)
  • PWM Range: 0–100% (converted to 0–255 internally)
  • Servo Range: 0–180 degrees
  • RGB Color Values: 0–255 per channel
  • I2C Address Range: 0x08–0x77
  • Browser Support: Chrome, Edge, Opera, or other Chromium browsers on desktop

🔗 Related Extensions #

  • 🔌 Hardware Extensions – Arduino, micro:bit, PRB, Pbot, Lego Boost, Lego WeDo 2.0, EV3
  • 🧠 AI Extensions – FaceMesh, HandPose, PoseNet, Image Trainer, Speech Recognition, ChatGPT, Speech to Text, Text to Speech

📚 Learn More #


Scroll to Top