AI Vision with PictoBlox, lesson 13 of 15

AI switches on a light: PictoBlox with Arduino

intermediate9 min read

Introduction

So far everything has happened on the screen. Now let's make AI control something in the real world! You'll connect an Arduino Uno to PictoBlox and wire up an LED. When the camera sees you smile, the LED switches on. Frown, and it switches off.

This is how real smart machines work: the camera sees, the AI thinks, and a motor, buzzer or light acts.

Your hand is the remote control. The model in PictoBlox sees a thumbs up, PictoBlox sends a message down the USB cable, and the Arduino turns the LED on.

Working principle

  • In Stage mode, PictoBlox talks to the Arduino all the time through the USB cable. The camera and the AI run on the laptop; the Arduino just follows orders.
  • The first time, PictoBlox puts a small program called firmware on the Arduino so it can understand those orders.
  • Set digital pin (13) output as (HIGH) switches pin 13 on (5 volts). LOW switches it off.
  • An LED needs a 220 Ω resistor in series, so too much current doesn't burn it out.
  • An LED only works one way round: the long leg (+) goes towards the pin, the short leg (−) goes to GND.

What you'll need

  • Arduino Uno and its USB cable
  • 1 LED (any colour)
  • 1 × 220 Ω resistor (red, red, brown bands)
  • A breadboard and 2 jumper wires
  • PictoBlox with the Face Detection extension and a webcam

Circuit diagram

Connect the parts like this:

Arduino pin 13 ──── 220 Ω resistor ──── LED long leg (+)
                                        LED short leg (−) ──── Arduino GND
  1. Push the LED into the breadboard with its legs in two different rows.
  2. Connect one end of the resistor to the row with the LED's long leg.
  3. Use a jumper wire from the other end of the resistor to pin 13.
  4. Use a jumper wire from the row with the LED's short leg to GND.

If you've never wired an LED before, start with Getting started for visual coding with Arduino.

Working in real life

  1. Plug the Arduino into your laptop with the USB cable.
  2. In PictoBlox, click Board and choose Arduino Uno.
  3. Click Connect, pick the Arduino's port (on Windows it's called something like COM3) and connect.
  4. Make sure you're in Stage mode (not Upload). If PictoBlox asks to upload firmware, say yes and wait for it to finish.
  5. Add the Face Detection extension and build the script below.
  6. Click the green flag. Smile at the camera: the LED lights up! Stop smiling and it goes off.

Code

when green flag clicked
turn [on] video on stage with (0) % transparency
forever
  analyse image from [camera]
  if <is expression of face (1) [happy]?> then
    set digital pin (13) output as [HIGH]
    say [Smile detected! Light on.]
  else
    set digital pin (13) output as [LOW]
    say [No smile, light off.]
  end
end

Try it

  • Add a second LED on pin 12 that lights up when you look surprised.
  • Swap face detection for the Human Body Detection blocks: switch the LED on when you raise your hand above your head.
  • Add a buzzer on pin 8 and make your rock, paper, scissors game beep when you win.

Check what you learnt

  1. In Stage mode, where does the AI run?

  2. Why does the LED need a 220 Ω resistor?

  3. Which LED leg goes towards pin 13?

Conclusion

  • PictoBlox in Stage mode lets AI on your laptop control an Arduino through the USB cable.
  • Set digital pin HIGH or LOW to switch an LED on or off, with a 220 Ω resistor to keep it safe.
  • See → think → act: now the "act" happens in the real world.

What next?

With great power comes great responsibility: Fair and safe AI: be a responsible AI maker.