Proximity Sample

This sample demonstrates measuring proximity.

To run the sample:

  1. Copy the sample code to a folder on the Raspberry Pi.

  2. Open terminal, and then go to the folder you copied the sample.

  3. Run the sample with the following command:

python3 Proximity.py

Sample Code

Proximity.py
#!/usr/bin/env python3

#This sample demonstrates measuring proximity.
#Install IoT HAT 3 library with "pip3 install turta-iothat3"

from time import sleep
from turta_iothat3 import Turta_Light

#Initialize
light = Turta_Light.LightSensor(Turta_Light.MODES.PROX)

try:
    while True:
        #Read proximity
        proximity = light.read_proximity()

        #Print the reading
        print("Proximity.......: " + str(proximity))

        #Wait
        sleep(0.5)

#Exit on CTRL+C
except KeyboardInterrupt:
    print('Bye.')

Result

When you run the sample, it prints the proximity measurement. The example output should be like this:

Proximity.......: 28
Proximity.......: 45
Proximity.......: 65
Proximity.......: 120

To stop the Python script, just press CTRL + C.

Last updated