Table of Contents

moveservo()

Function Definition: moveservo(servo_pin = "Servo 1", angle = 90)

Parameters

NameTypeDescriptionExpected ValuesDefault Value
servo_pinstringThe specific pin where the servo motor is connected."Servo 1", "Servo 2", "D1", "D2", or "D3""Servo 1"
angleintThe angle at which the servo needs to be set.0 to 18090

Description

The function sets the servo motor connected to the specified servo pin to the specified angle.

Example

The example demonstrates how to code the Quarky to make the servo sweep the perimeter in the Python Coding Environment.

Code

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

Angle = 0
while True:
  for i in range(0, 18):
    Angle += 10
    moveservo("Servo 1", Angle)
    time.sleep(0.01)

  for i in range(0, 18):
    Angle += -10
    moveservo("Servo 1", Angle)
    time.sleep(0.01)

Output

Read More
The example demonstrates how to calibrate the servo motor with Quarky in Python Coding Environment.

The purpose of servo motor calibration is to align the angle of your servo motor properly.

Connecting Servo to Quarky

The Servo motor will be connected to the Quarky Servo Connector. There are two servo ports on Quarky. Always make sure that brown wire is on your left side.

Code

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

quarky.moveservo("Servo 1", 90)

 

Put the Ultrasonic Assembly on the servo shaft.

Read More
Learn how to control the door of the IoT House with Python. Follow this step-by-step guide to control the servo motor and make the door open and close when you press the space key.

In this example, we will demonstrate how to control the door of the IoT House.

Circuit

Connect the servo motor to the Quarky Expansion Board servo pin 5.

Door Control

The door of the IoT House is controlled with a servo motor. You need to make the servo motor set to 0 angles before assembling the door. You can do it with the following code.

#Creating two objects called "quarky" and "expansion"
quarky = Quarky()

# The "expansion" object is now set to the "Expansion" class
expansion = Expansion()

# We are using the "moveservo" method from the "Expansion" class to make the servo motor 5 be set at 0-degree 
expansion.moveservo(5, 0)

Door Control Python Code

The following script makes the door closed by default and opens it for 1 second when the space key is pressed.

import time

sprite = Sprite('Tobi') # create a sprite object called 'Tobi'
quarky = Quarky() # create a Quarky object
expansion = Expansion() # create an Expansion object

expansion.moveservo(5,100); # move the servo on pin 5 to position 100

while True: # loop forever
	if sprite.iskeypressed("space"): # if the spacebar is pressed
		expansion.moveservo(5,0); # move the servo on pin 5 to position 0
		time.sleep(1) # wait for 1 second
		expansion.moveservo(5,100); # move the servo on pin 5 to position 100

Output

Press the space key to make the door open.

Read More
This project uses face recognition and an IR sensor to identify and authenticate authorized people and open the door accordingly. Learn how to create this system with Python code and hardware.

The project uses face recognition to identify authorized people and opens the door accordingly.

Circuit

We are using 2 devices in this project:

  1. IR Sensor: The IR sensor provides information if there is an obstacle in front or not. The IR sensor connections are as follows:
    1. GND Pin connected to GND of the Quarky Expansion Board.
    2. VCC Pin connected to VCC of the Quarky Expansion Board.
    3. Signal Pin connected to D3 of the Quarky Expansion Board.
  2. Servo Motor: The servo motor controls the Door of the IoT house which is connected to the servo port of 5 of the Quarky Expansion Board.

Alert: Make sure you have the Door Servo Motor calibrated.

Face Recognition

We will be using Face Detection extension for making the face recognition application.

Storing the Face Authorised for IoT House

This code is used to add a new face to a system:

  1. The first step is to create a Sprite object with the nameTobi‘. Then, a Face Detection object is created. The time library is imported.
  2. Then, a function called addFace() is defined. This function allows us to add a new face to the system. First, the video feed from the camera is turned on. Then, the camera is analyzed for a face. If one face has been detected, the user is asked to select a slot (1 to 10) and enter a name for the new face which is then added to the system. Finally, the video feed from the camera is turned off.
  3. The code runs a loop, which checks if thea key has been pressed. If it is, the addFace() function is called.
#Create a new Sprite object with the name 'Tobi'
sprite = Sprite('Tobi')

#Create a new Face Detection object
fd = FaceDetection()

#Import the time library
import time

#Set the threshold for face detection to 0.5
fd.setthreshold(0.5)
#Turn off the video feed from the camera
fd.video("off", 0)
#Enable the box to be drawn around the detected face
fd.enablebox()

#Define a function that adds a new face to the system
def addFace():
  #Create a flag to keep track if a new face has been added
  faceFlag = 0

  #Turn on the video feed from the camera
  fd.video("on", 0)
  time.sleep(1)

  #Keep looping until a new face has been added
  while faceFlag == 0:
    #Analyse the camera for a face
    fd.analysecamera()

    #Check if one face has been detected
    if fd.count() == 1:
      #Ask the user which slot the face should be added to
      sprite.input("Select the slot (1 to 10)?")
      #Store the slot number the user provided
      faceSlot = sprite.answer()

      #Ask the user to enter a name for the new face
      sprite.input("Enter the name of the face")
      #Store the name the user provided
      faceName = sprite.answer()

      #Add the face to the system with the provided slot number and name
      fd.addclassfromcamera(faceSlot, faceName)

      #Set the faceFlag to 1 to stop the loop
      faceFlag = 1

  #Turn off the video feed from the camera
  fd.video("off", 0)

#Keep running the loop forever
while True:
  #Check if the 'a' key has been pressed
  if sprite.iskeypressed("a"):
    #If yes, call the addFace() function
    addFace()

Working of an IR Sensor

An Infrared sensor is a type of sensor that senses if something is close to it or not. The IR stands for Infrared sensor. Infrared is the light out of our visible spectrum.

An IR sensor has a white LED (transmitter) and a photodiode (receiver). The transmitter emits IR light, and the receiver detects reflected light from objects within the sensor’s range, which can be adjusted with a potentiometer. The sensor is indicated by two LED indicators, a power LED which is always on, and a signal LED which is on when an object is detected and off when nothing is detected.

The signal LED has two states or situations:

  1. ON (Active) when it detects an object
  2. OFF (Inactive) when it doesn’t detect any object

Python Code for IR & Face Recognition-Based Smart Door Opening System

This code creates a program that can add a new face to the system, and then recognize and authenticate the user:

  1. It libraries of Sprite, FaceDetection, Quarky, Expansion, and IoTHouse to perform these tasks. It also imports the time library for timing purposes.
  2. The program sets the threshold for face detection to 0.5, turns off the video feed from the camera, and enables the box to be drawn around the detected face.
  3. It also moves a servo on the expansion board to position 5 and moves it to 100 degrees to close the door.
  4. It defines two functions called addFace() and authenticate().
  5. The authenticate() function turns on the video feed from the camera, recognizes the face in the camera, and speaks out the name of the recognized user if the face has been recognized. It then returns 1 to indicate the user has been authenticated.
  6. The program then keeps running the loop forever. It checks if thea key has been pressed and if yes, calls the addFace() function.
  7. It also checks if the IR sensor is active and if yes, calls the authenticate() function. If the user has been authenticated, it moves the servo to 0 degrees to open the door and then back to 100 degrees to close the door after some time.
#Create a new Sprite object with the name 'Tobi'
sprite = Sprite('Tobi')

#Create a new Face Detection object
fd = FaceDetection()

#Import the time library
import time

#Create a new Quarky object
quarky = Quarky()

#Create a new Expansion object
expansion = Expansion()
house = IoTHouse()

#Set the threshold for face detection to 0.5
fd.setthreshold(0.5)
#Turn off the video feed from the camera
fd.video("off", 0)
#Enable the box to be drawn around the detected face
fd.enablebox()

#Move a servo on the expansion board to position 5 and move it to 100 degrees
expansion.moveservo(5, 100);

#Define a function that adds a new face to the system
def addFace():
  #Create a flag to keep track if a new face has been added
  faceFlag = 0

  #Turn on the video feed from the camera
  fd.video("on", 0)
  time.sleep(1)

  #Keep looping until a new face has been added
  while faceFlag == 0:
    #Analyse the camera for a face
    fd.analysecamera()

    #Check if one face has been detected
    if fd.count() == 1:
      #Ask the user which slot the face should be added to
      sprite.input("Select the slot (1 to 10)?")
      #Store the slot number the user provided
      faceSlot = sprite.answer()

      #Ask the user to enter a name for the new face
      sprite.input("Enter the name of the face")
      #Store the name the user provided
      faceName = sprite.answer()

      #Add the face to the system with the provided slot number and name
      fd.addclassfromcamera(faceSlot, faceName)

      #Set the faceFlag to 1 to stop the loop
      faceFlag = 1

  #Turn off the video feed from the camera
  fd.video("off", 0)


#Define a function that authenticates the user
def authenticate():
  #Turn on the video feed from the camera
  fd.video("on", 0)
  time.sleep(1)

  #Recognise the face in the camera
  fd.recognisefromstage()

  #Check if one or more face has been detected
  if fd.count() > 0:
    #Loop through all the detected faces
    for i in range(1, fd.count() + 1):
      #Check if the face has been recognised
      if fd.getclassname(i) != "unknown":
        #Speak out the name of the recognised user
        sprite.say("Authorised - " + fd.getclassname(i), 2)

        #Turn off the video feed from the camera
        fd.video("off", 0)

        #Return 1 to indicate the user has been authenticated
        return 1
  
  #Turn off the video feed from the camera
  fd.video("off", 0)
  #Return 0 to indicate the user has not been authenticated
  return 0

#Keep running the loop forever
while True:
  #Check if the 'a' key has been pressed
  if sprite.iskeypressed("a"):
    #If yes, call the addFace() function
    addFace()

  #Check if the space key has been pressed
  if house.irstatus("D3"):
    #If yes, call the authenticate() function
    if authenticate() == 1:
      #Move the servo to 0 degrees
      expansion.moveservo(5, 0)
      time.sleep(2)
      #Move the servo back to 100 degrees
      expansion.moveservo(5, 100)

 

Output

Read More
This project demonstrates how to interface an RFID sensor with a Quarky to control the door of an IoT-enabled house using an authorized RFID tag. Learn how to write the code for RFID authentication and see the circuit diagram for connecting the RFID sensor to the Quarky board.

This project demonstrates how to interface an RFID sensor with a Quarky to control the door of an IoT-enabled house using an authorized RFID tag.

RFID to Quarky Circuit

Note: We are connecting the RFID sensor directly to Quakry Board.

RFID is short for “radio-frequency identification” and points to a technology whereby a reader catches digital information encoded in RFID tags. RFID sensors have a lot of pins. You have to connect it according to the following:

  1. The GND of the RFID sensor is connected to the GND of Quakry.
  2. The 3.3V of the RFID sensor is connected to the V of Quakry.
  3. The SDA Pin of the RFID sensor is connected to the A2 Pin of Quakry.
  4. The SCK Pin of the RFID sensor is connected to the D1 Pin of Quakry.
  5. The MOSI Pin of the RFID sensor is connected to the D2 Pin of Quakry.
  6. The MISO Pin of the RFID sensor is connected to the D3 Pin of Quakry.

The servo motor is connected to the S1 of Quarky.

Making RFID Master Tag

The following code makes any RFID Tag a master card that can be authorized for security:

  1. Tobi is a sprite object that helps to create an RFID tag. A Quarky object and an IoTHouse object are also created.
  2. The IoTHouse object needs to be initialized and a flag (MasterFlag) is set to 0 to indicate if the RFID tag has been written yet.
  3. Tobi then asks the user for the name of the user for the RFID tag.
  4. A loop is created that will keep running until the RFID tag is written. Tobi will ask the user to put the RFID tag and then try to write it to the house.
    1. If it is successful, the MasterFlag is set to 1 and the master tag of the RFID is set. Finally, Tobi will let the user know the RFID tag is created.
    2. If the RFID tag couldn‘t be written, Tobi will ask the user to put the RFID tag again.
# Create a sprite object for 'Tobi'
sprite = Sprite('Tobi')

# Create a Quarky object
quarky = Quarky()

# Create an IoTHouse object
house = IoTHouse()

# Initialise the RFID tag
house.initialiserfid()

# Set a flag to indicate if the RFID tag has been written
MasterFlag = 0

# Ask the user for the name of the user for the RFID tag
sprite.input("What is the name of the user for this RFID tag?")

# Keep looping until the RFID tag is written
while MasterFlag == 0:
  # Ask the user to put the RFID tag
  sprite.say("Writing on RFID! Please put RFID tag.")
  
  # Try to write the RFID tag to the house
  if house.writetorfid(sprite.answer(), 2):
    # Set the MasterFlag to 1, indicating the RFID tag has been written
    MasterFlag = 1

    # Set the master tag of the RFID
    house.setmaster()

    # Let the user know the RFID tag is created
    sprite.say("RFID tag created", 2)

  # If the RFID tag couldn't be written
  else:
    # Ask the user to put the RFID tag again
    sprite.say("No tag detected, please put RFID tag", 2)

This is how it looks:

Code for RFID Authentication

This code makes the Quarky open the door when it reads a special RFID card:

  1. First, it imports the time library. Then, it creates a Quarky object and an IoTHouse object.
  2. The IoTHouse object is initialized with an RFID reader and it can read the RFID card.
  3. Then, it moves the servo of the door to 100 degrees to close the door.
  4. Inside the while loop, it checks if the RFID is read.
    1. If it is read then it checks if the data scanned is Quarky. If it is, it moves the servo to 0 and then draws a pattern on the Quarky Display. It then waits for two seconds and moves the servo back to 100. After that, it clears the display of the Quarky.
    2. If the scanned data is not Quarky, then it draws a different pattern on the Quarky object and waits for one second. After that, it clears the display of the Quarky.
# First, we import the time library
import time

# We also create a Quarky object
quarky = Quarky()

# We create an IoTHouse object called 'house'
house = IoTHouse()
# We initialise the RFID of the house object
house.initialiserfid()

# We move the servo of the Quarky object to 100
quarky.moveservo("Servo 1", 100)

# We create a while loop that will go on forever
while True:
  # Check if the RFID is read
  if house.readrfid(3):
    # Check if the scanned data is Quarky
    if (house.readscanneddata() == "Quarky"):
      # Move the servo to 0
      quarky.moveservo("Servo 1", 0)
      # Draw a pattern on the Quarky Display
      quarky.drawpattern("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
      # Sleep for 2 seconds
      time.sleep(2)
      # Move the servo to 100
      quarky.moveservo("Servo 1", 100)
      # Clear the display of the Quarky Display
      quarky.cleardisplay()
    
    # If the scanned data is not Quarky
    else:
      # Draw a different pattern on the Quarky object
      quarky.drawpattern("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
      # Sleep for 1 second
      time.sleep(1)
      # Clear the display of the Quarky object
      quarky.cleardisplay()

Output

Read More
Learn how to set up a flame sensor with Quarky, calibrate it, and create a fire alarm system with a motor fan, door servo motor, and IFTTT webhooks. Get step-by-step instructions, circuit diagrams, python code, and more.

This example demonstrates how to set up the flame sensor with Quarky to detect heat or flame nearby. Later, we create an alarm system triggered with the flame sensor.

Flame Sensor Connection to Quarky

Flame sensors have 4 pins: GND, VCC, DO, and AO. You have to connect the following 3 pins to the Quarky Expansion Board:

  1. GND to Ground Pin of Quarky Expansion Board
  2. VCC to 3.3V or VCC Pin of Quarky Expansion Board
  3. DO to the D3 (Digital Pin) of the Quarky Expansion Board

Calibrating Flame Sensor

The sensor also has 2 LEDs – Power and Detection. The desired calibration is achieved when the sensor is inactive when there is no heat or flame nearby and active when the flame is nearby. It is visible on the detection LED.

To calibrate the flame sensor:

  1. Turn the power on for the sensor.
  2. Place the sensor close to the heat or flame. You should see the detection LED turn on.
  3. If the LED is Off, adjust the potentiometer until the detection LED turns on.
  4. Move the sensor away from the heat or flame. The detection LED should turn off.
  5. If the detection LED does not turn off, continue to adjust the potentiometer until it does.

Project: Flame-based Alarm System

In the project, when heat or flame is detected, the alarm system starts with

  1. The fan turned ON.
  2. Quakry beeping with lights.
  3. The door is open for urgent evacuation.

The alarm system will be on until the flame sensor stops detecting the fire.

Circuit

Connect the following modules to the Quarky Expansion Board:

  1. Flame Sensor
    1. GND to Ground Pin of Quarky Expansion Board
    2. VCC to 3.3V or VCC Pin of Quarky Expansion Board
    3. DO to the D3 (Digital Pin) of the Quarky Expansion Board
  2. Motor Fan: Connect the motor to the Motor Port 1 of the Quarky Expansion Board.
  3. Door Servo Motor: Connect the servo motor to the servo port 5 of the Quarky Expansion Board.

Python Code

  1. The code first creates objects of the Quarky, Expansion, and IoTHouse classes.
  2. It then moves the servo connected to pin 5 to 100 degrees and stops the motor connected to pin 1.
  3. Then it defines two functions, fireDetectedSequence() and fireStopSequence().
    1. When the fire is detected, it moves the servo connected to pin 5 to 0 degrees, runs the motor connected to pin 1 in clockwise direction with speed 100, plays a tone at C4 pitch for 8 beats, and draws a red pattern on the Quarky display.
    2. When the fire is no longer detected, it stops the motor connected to pin 1, clears the display of the Quarky robot, and moves the servo connected to pin 5 to 100 degrees.
  4. In the while loop, the code keeps on checking the flame status of pin D3 in the house.
    1. If the flame is detected, it waits for two seconds and again checks the flame status. If the flame is still detected, it runs the fireDetectedSequence() and then runs the fireStopSequence().
# The following code is written to detect a fire in a house using a Quarky robot and Expansion board.
# quarky is an instance of the Quarky class which has functionalities like playing a tone and drawing a pattern on LED Screen
quarky = Quarky()
# quarkyexpansion is an instance of the Expansion class which has functionalities like moving a servo and running a motor
quarkyexpansion = Expansion()
# house is an instance of the IoTHouse class which has functionalities like checking the flame status
house = IoTHouse()
# import time library which has functionalities like sleeping for a certain amount of time
import time

# move the servo connected to pin 5 to 100 degrees
quarkyexpansion.moveservo(5, 100)
# stop the motor connected to pin 1
quarkyexpansion.stopmotor(1)


# define a function which initiate instructions when the flame is detected
def fireDetectedSequence():
  # move the servo connected to pin 5 to 0 degrees
  quarkyexpansion.moveservo(5, 0)
  # run the motor connected to pin 1 in clockwise direction with speed 100
  quarkyexpansion.runmotor(1, 1, 100)
  # keep on checking the flame status of pin D3 in the house, until it is no longer in flame
  while not (house.flamestatus("D3")):
    # clear the display of the Quarky robot
    quarky.cleardisplay()
    # play a tone at C4 pitch for 8 beats
    quarky.playtone("C4", 8)
    # draw red pattern on the Quarky display
    quarky.drawpattern("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
    time.sleep(0.7)


# define a function which is initiated when no flame is detected
def fireStopSequence():
  # move the servo connected to pin 5 to 100 degrees
  quarkyexpansion.moveservo(5, 100)
  # stop the motor connected to pin 1
  quarkyexpansion.stopmotor(1)
  # clear the display of the Quarky robot
  quarky.cleardisplay()


while True:
  # keep on checking the flame status of pin D3 in the house
  if not (house.flamestatus("D3")):
    time.sleep(2)
    # again check the flame status of pin D3 in the house
    if not (house.flamestatus("D3")):
      # if flame is detected, run the fireDetectedSequence()
      fireDetectedSequence()
      # and then run the fireStopSequence()
      fireStopSequence()

Adding IoT in Fire Alarm System

As an advanced system, we can also send the fire detection alert to the users using IFTTT. For that, we will use IFTTT webhooks.

The following IFTTT sequence is to be created:

You can learn in detail how to create an IFTTT applet here: https://ai.thestempedia.com/extension/ifttt-webhooks/

Code

This code is the continuation of the past code:

  1. The code will continuously check the flame status of pin D3 in the house.
  2. If the flame is detected, it will initiate the fireDetectedSequence() which will send an event to IFTTT Webhooks, move the servo connected to pin 5 to 0 degrees, run the motor connected to pin 1 in the clockwise direction with speed 100, clear the display of the Quarky robot, and play a tone at C4 pitch for 8 beats.
  3. Once the flame status is no longer detected, it will initiate the fireStopSequence() which will send an event to IFTTT Webhooks, move the servo connected to pin 5 to 100 degrees, stop the motor connected to pin 1, and clear the display of the Quarky robot.
# The following code is written to detect a fire in a house using a Quarky robot and Expansion board.
# quarky is an instance of the Quarky class which has functionalities like playing a tone and drawing a pattern on LED Screen
quarky = Quarky()
# quarkyexpansion is an instance of the Expansion class which has functionalities like moving a servo and running a motor
quarkyexpansion = Expansion()
# house is an instance of the IoTHouse class which has functionalities like checking the flame status
house = IoTHouse()
# import time library which has functionalities like sleeping for a certain amount of time
import time
#Create an instance of the IFTTTWebhooks library
ifttt = IFTTTWebhooks()

# move the servo connected to pin 5 to 100 degrees
quarkyexpansion.moveservo(5, 100)
# stop the motor connected to pin 1
quarkyexpansion.stopmotor(1)

#Set the webhook key and event name
ifttt.setifttt("Flame_Detected", "iNyFg77wDLYV-V9UtdXVtmeebiOw_72LjxZud084ybr")

# define a function which initiate instructions when the flame is detected
def fireDetectedSequence():
  #Set the message and priority
  ifttt.setvalues("Fire Started! Evacuation Started", 1)
  ifttt.triggerevent() #Send the event
  # move the servo connected to pin 5 to 0 degrees
  quarkyexpansion.moveservo(5, 0)
  # run the motor connected to pin 1 in clockwise direction with speed 100
  quarkyexpansion.runmotor(1, 1, 100)
  # keep on checking the flame status of pin D3 in the house, until it is no longer in flame
  while not (house.flamestatus("D3")):
    # clear the display of the Quarky robot
    quarky.cleardisplay()
    # play a tone at C4 pitch for 8 beats
    quarky.playtone("C4", 8)
    # draw red pattern on the Quarky display
    quarky.drawpattern("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
    time.sleep(0.7)


# define a function which is initiated when no flame is detected
def fireStopSequence():
  #Set the message and priority
  ifttt.setvalues("Fire Stopped", 1)
  ifttt.triggerevent() #Send the event
  # move the servo connected to pin 5 to 100 degrees
  quarkyexpansion.moveservo(5, 100)
  # stop the motor connected to pin 1
  quarkyexpansion.stopmotor(1)
  # clear the display of the Quarky robot
  quarky.cleardisplay()


while True:
  # keep on checking the flame status of pin D3 in the house
  if not (house.flamestatus("D3")):
    time.sleep(2)
    # again check the flame status of pin D3 in the house
    if not (house.flamestatus("D3")):
      # if flame is detected, run the fireDetectedSequence()
      fireDetectedSequence()
      # and then run the fireStopSequence()
      fireStopSequence()
Read More
All articles loaded
No more articles to load