Required Equipment:
- ADS1015 12-Bit ADC - 4 Channel with Programmable Gain Amplifier
Bookmark the following repositories:
Using a digital input, one can trigger arecord
to record for a particular duration. Go here to view the code.
This code allows one to set and trigger a fixed duration for recording (defaults to 30 [note: the unit of measure is seconds here]) via some button press. It's worth pointing out that this exact same code could be used with any other digital input device with no alterations.
To use this code we need to separate files. One, video_player.py
provides the class information for Recorder
, which uses a Python library called subprocess
to wrap simple command line commands in Python code.
The pi video control repository contains a number of examples for controlling video playback, and recording.
Most of the examples should require little-to-no extra steps to use with the exception of threshold_video_trig. A description of the steps necessary to use this code follows:
-
turn on
i2c
inraspi-config
:sudo raspi-config
, selectInterfacing Options
, selectI2C
, clickYes
to enable. -
download i2c-tools:
sudo apt-get install -y i2c-tools
-
reboot for changes to take effect:
sudo reboot now
-
Connect the ADC to the Pi as follows:
- ADS1x15 VDD to Raspberry Pi 3.3V
- ADS1x15 GND to Raspberry Pi GND
- ADS1x15 SCL to Raspberry Pi SCL
- ADS1x15 SDA to Raspberry Pi SDA
-
Test to confirm that the pi sees your
i2c
device:sudo i2cdetect -y 1
If everything is working you should see something like this:0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
-
Install the
adafruit-ads1x15
library frompip3
:sudo pip3 install adafruit-ads1x15
-
Clone the github library so we have access to Adafruit's examples:
git clone https://github.com/adafruit/Adafruit_Python_ADS1x15.git
-
cd
into the examples directory -
Make
simpletest.py
executable:sudo chmod +x simpletest.py
-
Confirm functionality by running
simpletest.py
with Python3 (apparently we need to usesudo
here):sudo python3 simpletest.py
-
Ctl-C to exit once you have confirmed that this works.
The following example repurposes trig_play.py
from the pi video control to trigger video playback when an analog sensor passes a fixed threshold
:
#!/usr/bin/python3
'''
trigger video playback when analog sensor crosses a value
'''
from video_player import *
import time
import Adafruit_ADS1x15
BASE_DIR = '/home/cta/'
FILENAME = 'dramatic_chipmunk.mp4'
play_path = ''.join([BASE_DIR, FILENAME])
play = Player(play_path)
adc = Adafruit_ADS1x15.ADS1015()
GAIN = 1
threshold = 500
is_playing = False
try:
print("reading sensor values!")
while True:
sensor_val = adc.read_adc(0, gain=GAIN)
if is_playing == False and sensor_val <= threshold:
print('threshold crossed, starting video')
play.play()
is_playing = True
elif is_playing == True:
if play.status() == 'done':
print(''.join(['done', '\n', '\n']))
is_playing = False
except KeyboardInterrupt:
print(''.join([ '\n', '\n', 'INTERRUPTED', '\n']))
if play.status() == 'playing':
print('video is running, terminating now!')
play.kill()
else:
print('no video running, exiting now')