How to Use IR Sensors in Quarky Robot

Description
Learn how to use Infrared (IR) sensors in the Quarky robot with PictoBlox Block and Python coding environment. Discover how to calibrate the IR sensors and make interactive robots with line-following and hand-sensing capabilities.

Introduction

Quakry has 2 Infrared (IR) sensors on the back side of the Quarky.

How does IR Sensor work?

An IR sensor consists of 2 LEDs: one which transmits the IR light and one which receives the IR light. When the IR rays are transmitted, they bounce from the nearest surface and get back to the receiver LED. That’s how an IR sensor detects an object.

To detect colors, the IR sensor depend on the number of rays the surface reflects:

  1. The dark surface will absorb more IR rays and as a result, the receiver will get fewer IR rays.
  2. White or shiny objects will absorb fewer IR rays and as a result, the receiver will get more IR rays.

We can get the sensor values in PictoBlox and based on that value we can estimate whether the surface is black or white.

  1. If the sensor detects the black line, its output value is increased. This means that the sensor is active.
  2. If it detects the white area, its output value decreases. This means that the sensor is inactive.

We will call the threshold value above which the sensor detects the black line. If the sensor value is less than the threshold, it means that the sensor hasn’t detected the line yet.

evive Alert
Alert: IR sensors DON’T work in sunlight. IR rays from the sun increase the overall threshold of the sensors therefore they stay active all time. A closed environment or nighttime is the place/time to work with your line-following robot.

Block Coding

In PictoBlox, you have the following blocks available for the IR sensor in the Sensing Extension:

  1. is () IR sensor active?: The block returns the state of the specified IR sensor. It returns True if the current IR sensor value is greater than the threshold value, else False.
  2. get value of (): The block returns the IR sensor analog reading. The reading varies from 0 to 4095.
  3. set () IR sensor threshold to (): The block sets the threshold value of the specified IR sensor to the specified value. The value can be from 0 to 4095.
Note: Make sure you have the latest version of the firmware uploaded to Quarky. You can learn more here: https://ai.thestempedia.com/docs/quarky/quarky-toubleshooting/updating-quarky-firmaware-with-pictoblox/

Example 1: IR Sensor Calibration (Stage Mode)

The example demonstrates how to calibrate the IR sensors to detect black lines on the white surface in Stage Mode.

Note: The Stage mode is a mode of working in PictoBlox in which we can interact with Quarky in real-time. If you disconnect the board with Pictoblox, you can no longer interact with the Quarky. In this mode, you can make games and animations by interacting with Quarky or any other prototyping board.

Script

 

Calibrating the IR Sensors

Now, run the script by clicking the green flag and bringing the black line of the track close to the IR sensor. One of the following three conditions will happen:

  1. Your calibration value is HIGH: The black region will not be detected in this case. Reduce the calibration value.
  2. Your calibration value is LOW: The white region will not be detected in this case. Therefore, increase the calibration value.
  3. Your calibration value is OK. The white and black regions are detected accurately.

Now modify the script to add the detection value for the right IR sensor.

Example 2: Robot Pet (Upload Mode)

The example demonstrates how to make a vertical robot pet that senses the hand on the IR sensor and acts accordingly.

Note:  Upload mode is one of the two modes you can write your programs in Pictoblox. This mode allows you to write scripts and upload them to the board so that you can use them even when it is not connected to your computer. For example, you need to upload a script for making moving robots.

Script:

Python Coding

In PictoBlox, you have the following Python function available for the IR sensor:

  1. readirsensor(): The function returns the IR sensor analog reading. The reading varies from 0 to 4095.
  2. getirstate(): The function returns the state of the specified IR sensor. It returns True if the current IR sensor value is greater than the threshold value, else False.
  3. setirthreshold(): The function sets the threshold value of the specified IR sensor to the specified value. The value can be from 0 to 4095.

Example 1: IR Sensor Calibration in Python (Stage Mode)

The example demonstrates how to calibrate the IR sensors to detect black lines on the white surface in Python Coding Environment in the stage mode.

Note: The Stage mode is a mode of working in PictoBlox in which we can interact with Quarky in real-time. If you disconnect the board with Pictoblox, you can no longer interact with the Quarky. In this mode, you can make games and animations by interacting with Quarky or any other prototyping board.
sprite = Sprite('Tobi')

quarky = Quarky()

quarky.cleardisplay()
quarky.setirthreshold("IRL", 3000)
quarky.setirthreshold("IRR", 3000)

while True:
  if quarky.getirstate("IRL"):
    quarky.setled(1, 1, [0, 255, 0], 20)
  else:
    quarky.setled(1, 1, [255, 0, 0], 20)
  
  if quarky.getirstate("IRR"):
    quarky.setled(7, 1, [0, 255, 0], 20)
  else:
    quarky.setled(7, 1, [255, 0, 0], 20)

Calibrating the IR Sensors

Now, run the code by clicking the green flag and bringing the black line of the track close to the IR sensor. One of the following three conditions will happen:

  1. Your calibration value is HIGH: The black region will not be detected in this case. Reduce the calibration value.
  2. Your calibration value is LOW: The white region will not be detected in this case. Therefore, increase the calibration value.
  3. Your calibration value is OK. The white and black regions are detected accurately.

Now modify the script to add the detection value for the right IR sensor.

Example 2: Robot Pet in Python (Upload Mode)

The example demonstrates how to make a vertical robot pet that senses the hand on the IR sensor and acts accordingly in the Python Coding Environment.

Code:

from quarky import *

# imported modules
import time
from quarkyConstants import *

quarky.setorientation(2)
quarky.setirthreshold("IRL", 3000)
quarky.setirthreshold("IRR", 3000)
while True:
  if quarky.getirstate("IRL"):
    if quarky.getirstate("IRR"):
      quarky.cleardisplay()

    else:
      pass
      quarky.runrobot("LEFT", 100)
      time.sleep(0.3)
      quarky.stoprobot()

  else:
    pass
    if quarky.getirstate("IRR"):
      quarky.runrobot("RIGHT", 100)
      time.sleep(0.3)
      quarky.stoprobot()

    else:
      pass
      quarky.showemotion("happy")
      quarky.playsounduntildone("QuarkyIntro")

Conclusion

In this lesson, we learned about how to use the IR sensors in the Quarky robot. We saw how to make a linefollowing robot and a robot pet that senses the hand on the IR sensor and acts accordingly. We also learned how to calibrate the IR sensors in both PictoBlox (Block Coding) and Python Coding Environment for both Stage and Upload modes. This is an essential skill for any robotics enthusiast who wants to build a linefollowing robot or make an interactive robot.

Table of Contents