Introduction to Ultrasonic Sensors and Quarky

Description
Learn all about ultrasonic sensors, how they work, and how to connect them to Quarky. Also, learn to code with PictoBlox and Python to detect obstacles and measure distances.

Introduction

An ultrasonic sensor is an electrical device that uses ultrasonic sound waves to determine the distance to a target item. It is a demanding instrument in robotics as well as for industrial purposes.

It is one of the most reliable ways to sense proximity and detect a target.

How do Ultrasonic Sensors Work?

It works by emitting a sound wave that travels through the air and bounces back to the sensor if it encounters an obstacle or object and thus calculates the distance by measuring time lapses between the sending and receiving of the ultrasonic pulse. This sound wave is higher in frequency than the normal human hearing range. Ultrasonic sensors have now found their way into a wide range of applications and industries.

Connecting Ultrasonic Sensor to Quarky

The sensor and the Quarky have the following pins:

  1. Ultrasonic Sensor Pins:
    1.  VCC
    2. GND
    3. Trig
    4. Echo
  2. Quarky Pins:
    1. GND
    2. V
    3. D1
    4. D2

We will start with connecting the ultrasonic sensor with Quarky using the 4 set wires provided in the kit. But, first, make the connection in the following way:

  • First, connect the VCC of the ultrasonic sensor with the V pin on the Quarky.
  • Connect the GND of the ultrasonic sensor with the Ground pin on the Quarky.
  • Connect the Trig of the ultrasonic sensor with the D1 pin on the Quarky.
  • Finally, connect the Echo of the ultrasonic sensor with the D2 pin on the Quarky.

Block Coding

In PictoBlox, you have the following blocks available for the ultrasonic sensor:

  1. connect ultrasonic () to trig () echo (): The block initializes the ultrasonic sensor with specified echo and trig pins. The block has three inputs:
    1. The sensor number. You can define 2 sensors at a time.
    2. The specific GPIO pin where the trig pin of the ultrasonic sensor is connected.
    3. The specific GPIO pin where the echo pin of the ultrasonic sensor is connected.
  2. get ultrasonic () distance (cm): The block returns the distance reading from the specified ultrasonic sensor.
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: Reading distance from  (Stage Mode)

The example demonstrates how to use an ultrasonic sensor to detect the distance with Quarky 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:

Note: If you are getting a 0 value, please check if the sensor wiring is correct.

Output:

To begin with, bring an object in front of the sensor and measure the distance.

Toby will say the distance value. The value will decrease as the hand approaches the sensor and increase as it moves away.

Example 2: Detect Obstacle from Ultrasonic Sensor (Upload Mode)

In this example, Quarky’s LED should change from green to red whenever an obstacle comes closer than 10cm.

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.

Logic:

The logic is simple. The robot will keep on checking the distance. If the distance is less than 10cm, the LEDs will glow RED; otherwise, they will glow GREEN. You can follow this logic in the flowchart below:

Script:

Python Coding

In PictoBlox, you have the following Python function available for the touch sensors:

  1. setultrasonicpins(sensor_number = 1, trig_pin = “D1”, echo_pin = “D2”): The function initializes the ultrasonic sensor with specified echo and trig pins.
  2. getdistance(sensor_number = 1): The function returns the distance reading from the specified ultrasonic sensor.

Example 1: Reading distance from (Stage Mode)

The example demonstrates how to use an ultrasonic sensor with Quarky.

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.

Code: 

sprite = Sprite('Tobi')
quarky = Quarky()

quarky.setultrasonicpins(1, 18, 19)
while True:
  sprite.say(quarky.getdistance(1))

Example 2: Detect Obstacle from Ultrasonic Sensor (Upload Mode)

In this example, Quarky’s LED should change from green to red whenever an obstacle comes closer than 10cm in Python Coding Environment.

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.

Code:

from quarky import *

# imported modules
import time

quarky.setultrasonicpins(1, 19, 18)
while True:
  if (quarky.getdistance(1) > 10):
    quarky.drawpattern("ccccccccccccccccccccccccccccccccccc")

  else:
    pass
    quarky.drawpattern("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")

  time.sleep(0.5)

Conclusion

In this lesson, we learned about ultrasonic sensors and how to use them with Quarky. We saw two examples of how to use them in both Stage and Upload modes with PictoBlox. We also saw two different coding languages to program the Quarky with the ultrasonic sensor. In conclusion, the ultrasonic sensor is a reliable and efficient way to detect a target and measure distances accurately.

Table of Contents