Program Quarky to Detect Ambient Light with LDR Sensor

Example Description
Learn how to program Quarky to detect the ambient light reading and make the light turn ON and OFF accordingly.

In this example, we are going to learn how to program the Quarky to detect the ambient light reading and make the light turn ON and OFF accordingly.

LDR Sensor Connection to Quarky

LDR 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. AO to the A1 (Analog Pin) of the Quarky Expansion Board

Getting Sensor Reading

The following script displays the real-time sensor reading. The reading will vary from 0 to 4095.

# Create a Sprite object named 'Tobi'
sprite = Sprite('Tobi')

# Create a Quarky object
quarky = Quarky()

# Create an IoTHouse object
house = IoTHouse()

# Create an infinite loop
while True:
  # Have the Sprite say the LDR value of 'A1' in the IoTHouse
  sprite.say("Sensor Reading - " + str(house.ldrvalue("A1")))

 

Connect Quarky and you will start getting the readings.

You will notice from the reading that when you put your hands close to the sensor the value is higher. This is because the sensor receives less light and the voltage across the sensor increases. We will create a threshold to identify when the light is low.  For the above readings, it will be 2800. Now if the sensor value is greater than 2800, then it is active, meaning that the light is low. We will use it in the next code.

Automatic Light

  1. In this code, we are creating two objects: one called quarky and one called house.
  2. We are setting the brightness of quarky to 15 and the light threshold for a house to 2800.
  3. We have an infinite loop that checks the light status at sensor A1.
  4. If the light status at sensor A1 is true, it will light the Quarky Display with White Light. If the light status at sensor A1 is false, it will clear the display on the Quarky object.

 

Uploading Code

 

Click on the Upload Code button.

# This code uses two different libraries: 'quarky' and 'iothouse'. 
# 'iothouse' is used to detect the light intensity in a room, and 'quarky' is used to control LEDs.

# First, we set the brightness of the LEDs to 15.
from quarky import *
quarky.setbrightness(15)

# Then, we set the light level threshold to 2800.
import iothouse
house = iothouse.iothouse()
house.setldrthreshold(2800)

# Now, the program will check the light level continuously and take action based on the result.
while True:
  # If the light level is below the threshold, the program will draw a pattern on the LEDs.
  if house.ldrstatus("A1"):
    quarky.drawpattern("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")

  # If the light level is above the threshold, the program will clear the LEDs.
  else:
    quarky.cleardisplay()
Table of Contents