Things we are going to need:
- Raspberry Pi with Raspbian
- Touch Sensor from adafruit
- Power Source (Battery/DC)
- Ethernet or WiFi for Raspberry Pi
- Another computer
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Setting Up Our Raspberry Pi Hardware
First, connect our touch sensor to GPIO pins on our Raspberry Pi. Use some flexible cables
Identify 3 pins on our sensor:
- GND - connect to ground pin
- VCC - connct to 5V pin
- SIG - connect to a signel pin
We'll use the 5V, ground and GPIO 18 on our raspberry pi in this example.
Step 2: Setting Up Our Environment on the Raspberry Pi
Obtain the IP address of our raspberry Pi.
Then connect to our raspberry pi using ssh:
then type in our password.
Once we are in, then install apache2 as our webserver by typing in:
sudo apt-get install apache2
Step 3: Design a Piece of Python Code to Read Input From Our Sensor
In our example code, we use a couple of different commands.
To indicate that we're happy, we do 2 long presses.
To indicate that we're sad, we do one short tap, and one long press.
To indicate that we're confused, we do one short tap, wait a beat, then 2 short taps. To indicate that we're happy, we do one short tap, one short gap, and one long press.
To indicate that we're bored, we 3 short taps.
Translating these to string commands:(t for short tap, T for long press, g for short gap, G for long gap)
Then we'll have our code output our current mood into a browser-friendly html file for serving.
import RPi.GPIO as GPIO
GPIO.cleanup() GPIO.setmode(GPIO.BCM) GPIO.setup(18,GPIO.IN)
touch_count = 0 touch_state = 0 touch_duration = 0 gap_duration = 0 current_cmd = ""
def cmd(): global current_cmd if current_cmd[-3:] == "TgT": current_cmd = "" mood("Sad") if current_cmd[-3:] == "tgT": current_cmd = "" mood("Happy") if current_cmd[-5:] == "tGtgt": current_cmd = "" mood("Confused") if current_cmd[-5:] == "tgtgt": current_cmd = "" mood("Bored")
def mood(mood): file = open("index.html","w") html = """
Portable Mood Reporter
Hey, my most recent mood is
while(1): time.sleep(0.001) if(GPIO.input(18)): if touch_state==0: if gap_duration > 2000: gap_duration = 0 if gap_duration > 200: current_cmd = current_cmd + "G" else: current_cmd = current_cmd + "g" gap_duration = 0 touch_state = 1 touch_count = touch_count + 1 if touch_state==1: touch_duration = touch_duration + 1 else: if gap_duration < 10000: gap_duration = gap_duration + 1 if touch_state==1: touch_state = 0 if touch_duration > 200: current_cmd = current_cmd + "T" else: current_cmd = current_cmd + "t" touch_duration = 0 cmd()
Setup to work with our webserver
type or upload the above python file into our webserver location,
which is defaulted to /var/www/html
sudo nano touch.py
Then type in the code above
Step 4: Start Our Server to See It in Action!
sudo python touch.py
Then type in the IP address for our Raspberry Pi, then we should see our mood reporter working!
Try different types of touch commands, and the page should auto-refresh to reflect that!