Robotics-II-Circuit-Python

Circuit Python tutorials in Robotics II

View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python

Table of Contents

APDS-9960 Proximity Sensor Tutorial

In this tutorial, you’ll learn how to use the APDS-9960 sensor in proximity mode. This sensor can detect the presence of an object and measure how close it is to the sensor. You’ll learn how to wire the sensor, understand how it works, see a code example, and complete some challenges to test your understanding.

Video Tutorial


Text Tutorial

Wiring Diagram

To wire the APDS-9960 sensor using StemmaQT, follow these steps:

  1. Connect the Blue Wire (Data): Connect the blue wire to the data (SDA) pin on your microcontroller board.
  2. Connect the Yellow Wire (Clock): Connect the yellow wire to the clock (SCL) pin on your microcontroller board.
  3. Power and Ground:
    • Connect the red wire to the 3.3V or 5V power pin.
    • Connect the black wire to the ground (GND) pin.

How the Proximity Sensor Works

The APDS-9960 sensor includes a proximity sensor that can measure the distance to an object without physical contact.

I2C Communication

I2C (Inter-Integrated Circuit) is a communication protocol commonly used in microcontrollers and sensors to facilitate communication between devices. It allows multiple “slave” devices to communicate with one or more “master” controllers using only two wires, making it a simple and efficient method for data transfer. Effectively we could connect 20+ sensors to these single 2 pins and be able to interact with them all, rather than needing 20 individual digital/analog pins.

Key Components of I2C:

  1. SCL (Serial Clock Line):
    • SCL is the clock line that is controlled by the master device.
    • It synchronizes the data transfer between the master and slave devices.
    • Both the master and slave devices rely on SCL to know when to read or write data.
  2. SDA (Serial Data Line):
    • SDA is the data line used to transfer data between devices.
    • It is a bidirectional line, meaning it can be used by both master and slave devices to send and receive data.
    • Data on the SDA line is transferred in synchronization with the clock pulses on the SCL line.

How I2C Works:


Code Example

Here’s a basic code example to get started with the APDS-9960 sensor in proximity mode: Ensure you have the adafruit_adps9960 library on your CIRCUITPY Lib folder.

import board
import busio
import time
import adafruit_apds9960.apds9960

# Set up I2C connection
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_apds9960.apds9960.APDS9960(i2c)

# Enable proximity sensing
sensor.enable_proximity = True

while True:
    proximity = sensor.proximity
    print("Proximity:", proximity)
    time.sleep(1)

Challenges

Challenge 1: Detecting Object Presence

Use the proximity sensor to detect when an object is placed near the sensor. Try different objects and observe the proximity values.

Challenge 2: Proximity Alert

Create a function that triggers an alert when an object comes within a certain distance of the sensor.

Click to reveal a hint for Challenge 2 You can use a simple threshold, e.g., if proximity > 50, trigger an alert. Adjust the threshold based on your needs.