LEARN.PARALLAX.COM
Published on LEARN.PARALLAX.COM (https://learn.parallax.com)
Home > Control your cyber:bot with an Infrared TV Remote

Control your cyber:bot with an Infrared TV Remote

An Infrared Remote is the easiest way to add wireless control to your cyber:bot. This example lets you drive the robot nine different directions using the numbers on the remote. Once you know how the code works, you can extend the use of the remote control for other purposes. You could start the robot in a contest and then change the behavior: adjust speeds, enable different sensors, turn LEDs on or off. Take a look at the video below to see what you’re in for!

Before You Start

Build and test your cyber:bot, following all Prerequisite and Main tutorials up to Navigation with the cyber:bot. If you know how to program your cyber:bot with drive speeds to move different directions then you’re ready to try this project.

What’s Needed

  • (1) – Fully assembled and tested cyber:bot (#32700)
  • (1) – Universal Remote with manufacturer manual (#020-00001)
  • The following parts from your cyber:bot kit’s Electronics Component Pack (#572-28132)
    • (1) – IR detector (#350-00039)
    • (1) – 220 ohm Resistor (#150-02210)
    • (1) – Jumper wires (bag of 10) (#800-00016)

After you Finish

You will know how to write a script that makes your cyber:bot respond to remote button presses with the behaviors you assign.  That could be motion, LED matrix pictures, or sound effects.

Program the Remote for the cyber:bot

Let’s set up your remote!

The remote you are using must be configured to send Sony-compatible infrared signals. The cyber:bot Python code provided for this project makes the cyber:bot recognize Sony IR remote signals only!

  • First of all, make sure your remote has fresh batteries.
  • Then, program your remote for Sony protocol. To program the BRIGHTSTAR universal remote sold by Parallax.
    • Hold the SET-UP button down until an LED on the remote lights up
    • Enter the code 605.

Build the IR Receiver Circuit

If you have not used the IR Receivers with your cyber:bot before, take a look at the pictures below. The infrared receiver needs three connections: 5 V, GND, and an I/O pin to read the signal it sends.

  • Build the circuit as shown by this pictorial and schematic:

The circuitry inside the infrared receiver makes it safe to connect its signal pin to the cyber:bot PCB with a small resistor, even though the sensor is powered by 5 V

Test the IR Receiver Circuit

Let’s check that your infrared receiver circuit is wired properly and that your remote was programmed correctly. This program displays the numbers from the infrared remote on the micro:bit’s LED display. This example provides an easy way to determine the number associated with each key on the remote (especially the non-numbered CH and VOL keys).

Hardware Setup

  • Set the cyber:bot board’s power (PWR) switch to Position 0.
  • Make sure the battery holder is loaded with 5 AA batteries.
  • Make sure the battery holder’s barrel plug is firmly plugged into the cyber:bot board’s barrel jack. 
  • Connect your micro:bit module to your computer with a USB cable.

Software Setup

  • In a Google Chrome or Microsoft Edge browser, go to python.microbit.org to open the micro:bit Python Editor.
  • Add two modules to the project: cyberbot.py, qti.py, and intbits.py.
    • Go to Add modules to your micro:bit.
    • Skip the Quick Start section, and instead start at the section titled Adding a Module to micro:bit Filesystem. 
    • Watch the video and then add the cyberbot.py module to the project by following the instructions from there to the end of the page. 
    • Next, follow the same steps to add the tv_remote.py module to the project.  It will be in the same folder with cyberbot.py. 

Script: IR_Remote_Test

  • If you haven’t already done so, follow the instructions in the Software Setup section to add the cyberbot.py and tv_remote.py modules to your project.
  • Set the project’s name to ir_remote_test, enter the script below, and then click Save.
    (See Save & Edit Scripts and Flash Scripts with Python Editor.)
  • Click Send to micro:bit.  
    (See Flash Scripts with Python Editor.)
  • Set the cyber:bot board’s PWR switch in position 1.
  • Point the remote at the IR receiver on the board, and then press and release a number button on the remote. You should see the number displayed on the LED matrix.
# ir_remote_test.py            

from cyberbot import *
from tv_remote import *

bot(22).tone(2000, 300)

while True:
    num = ir(10).remote()
    if num > -1:
        display.scroll( str(num), 75, wait=False )
    sleep(100)

 

IR Interference — Are you seeing numbers appear on the micro:bit LED matrix when you are NOT pressing any numbers on the remote?

First, check to see if you are getting a syntax error report from your micro:bit. 

If that’s not the problem, your IR receiver might be getting signals similar to a TV remote’s signals from some other source. The most common culprit is overhead fluorescent light fixtures. If possible, turn them off or try taking your robot to a different area.

 

Drive your cyber:bot with the Remote

With the next program, use buttons 1-9 to drive the robot in different directions.  Once you press a button, your robot will drive in that direction until you choose another direction. Press 5 to make it stop.

  • Change the project’s name from ir_remote_test to ir_remote_control. 
  • Enter the script below, and then click Save.
  • Click Send to micro:bit.  
  • Unplug the USB cable from the cyber:bot.
  • Set the cyber:bot on the floor, and set the cyber:bot board’s PWR switch to 2.
  • Point the remote at the IR receiver on the board, and press numbers on the remote to make it drive. Press 5 to make it stop.
# ir_remote_control

from cyberbot import *
from tv_remote import *

wL = 0
wR = 0
img = Image.HAPPY

bot(22).tone(2000, 300)

while True:
    num = ir(10).remote()

    if num == 1:
        wL = 0
        wR = -75
        img = Image.ARROW_SE
    elif num == 2:
        wL = 75
        wR = -75
        img = Image.ARROW_S
    elif num == 3:
        wL=75
        wR=0
        img=Image.ARROW_SW
    elif num == 4:
        wL=-75
        wR=-75
        img=Image.ARROW_E
    elif num == 5:
        wL=0
        wR=0
        img=Image.HAPPY
    elif num == 6:
        wL=75
        wR=75
        img=Image.ARROW_W
    elif num == 7:
        wL=0
        wR=75
        img=Image.ARROW_NE
    elif num == 8:
        wL=-75
        wR=75
        img=Image.ARROW_N
    elif num == 9:
        wL=-75
        wR=0
        img=Image.ARROW_NW

    display.show(img)
    bot(18).servo_speed(wL)
    bot(19).servo_speed(wR)

 

How the IR Remote Control Code Works

IR Remote Signals

The remote flashes its IR LED on/off very rapidly—at 38.5 kHz—for certain periods of time, with periods of off-time in between. The combinations of these pulses are received by the micro:bit as the number pressed, not a series of pulses. How does this work exactly?

The IR receiver is sensitive to bursts of 38.5 kHz infrared light, and responds by outputting a 5V high signal.  The Propeller I/O pin, P10 in this case, measures how long each high signal lasts.  The Propeller’s cyberbot firmware interprets the high signal patterns, and then works with the tv_remote.py library to match the patterns to different buttons on your remote.

How IR_Remote_Control.py Works

Our program IR_Remote_Control.py begins by initializing the variables wL and wR to zero, and the variable img to image.HAPPY. After a quick beep, the bulk of the code is a while True: loop. Inside the loop, num = ir(10).remote() checks to see which remote button press is detected and stores the result in the variable num.  Next, a long if…elif series uses the value of num to select new values for  wL and wR as well as an image to display

The new values of wL, wR, and img are passed to the display.show(img), bot(18).servo_speed(wL) and bot(19).servo_speed (wR) calls, so the robot drives off with in a new direction with an updated display until the next new remote button press is detected.

More Actions for More Keys? — There are more buttons on the remote, so you can add more robot behaviors to your project, such as making sound effects on the piezospeaker. However, there is a catch! Keep in mind that when the cyber:bot is generating frequencies on the piezospeaker, it can’t do anything else, like check to see if you have pressed a new button on the remote. The exception is that the servos will maintain the last speed setting they received until they get a new one. 

Add Obstacle Detection

The script IR_Remote_Control.py makes your cyber:bot drive in whatever direction you send it — even into walls. If you have a Ping))) sensor, you can give your ’bot a bit of self-preservation behavior.

Additional parts needed:

  • Ping))) Ultrasonic Distance Sensor (#28015)
  • 2.2 k-ohm resistor from your robot’s Electronic Component Pack
  • Jumper wires

The Ping))) sensor can go directly into the breadboard.

  • Add the Ping))) sensor to the cyber:bot and connect its signal pin to P8, as shown below. You may need to re-position the IR receiver, but that is fine as long as its signal pin still connects to P10.

 

The script IR_Remote_Control_with_Ping is just an expansion of the original.  Up top, it adds from ping import *.   Then, it takes the entire contents of the while True: loop and wraps it in a nested conditional while loop.  This inner loop only executes while (ping(8).distance(’cm’)) > 15:.  If that condition is not true, meaning you have driven your cyber:bot close too an object, some new lines of code at the bottom of the conditional loop make the cyber:bot express its surprise and turn away until the obstacle is no longer detected.

  • You will need to add the ping.py module to your project, just like you added the cyberbot.py and tv_remote.py before testing the remote.
  • Change the project’s name from ir_remote_control to ping_servo_test. 
  • Enter the script below, and then click Save.
  • Click Send to micro:bit.  
  • Disconnect the cyber:bot robot’s USB cable, and set it on the floor.
  • Set the cyber:bot board’s PWR switcht to 2 and 
# ir_remote_control_with_ping

from cyberbot import *
from tv_remote import *
from ping import *

wL = 0
wR = 0
img = Image.HAPPY

bot(22).tone(2000, 300)

while True:

    while (ping(8).distance('cm')) > 15:
        num = ir(10).remote()
    
        if num == 1:
            wL = 0
            wR = -75
            img = Image.ARROW_SE
        elif num == 2:
            wL = 75
            wR = -75
            img = Image.ARROW_S
        elif num == 3:
            wL=75
            wR=0
            img=Image.ARROW_SW
        elif num == 4:
            wL=-75
            wR=-75
            img=Image.ARROW_E
        elif num == 5:
            wL=0
            wR=0
            img=Image.HAPPY
        elif num == 6:
            wL=75
            wR=75
            img=Image.ARROW_W
        elif num == 7:
            wL=0
            wR=75
            img=Image.ARROW_NE
        elif num == 8:
            wL=-75
            wR=75
            img=Image.ARROW_N
        elif num == 9:
            wL=-75
            wR=0
            img=Image.ARROW_NW
   
        display.show(img)
        bot(18).servo_speed(wL)
        bot(19).servo_speed(wR)
        
    display.show(Image.SURPRISED)
    bot(18).servo_speed(-50)
    bot(19).servo_speed(-50)
    bot(22).tone(500, 1000)
   

 

 

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024


Source URL:https://learn.parallax.com/courses/control-your-cyberbot-with-an-infrared-tv-remote/
Links