Vous êtes sur la page 1sur 4

HOW TOs:

To make an LED light blink, just follow these steps:


# GrovePi LED Blink example - using a hashtag, we can make personal comments
on the code
# http://www.seeedstudio.com/wiki/Grove_-_LED_Socket_Kit - this is another
comment which takes you to a page that tells you about the sensor
import time - this will record the current time, according to the computer's settings
from grovepi import * - this is the source of the import
# Connect the Grove LED to digital port D2 - again, this is a comment which tells
the coder where to connect the sensor in the Pi
led = 2 - this is just a value which labels the LED light as 2
led2 = 3 - this is another led value so it does not get confused with the first
pinMode(led, "OUTPUT") time.sleep(1) - this creates a pause between two actions
while True: - this means that if the following is correct/accurate, an action will take
place
try: - telling the Pi to do the following
#Blink the LED - another comment for the coder's use
digitalWrite(led, 1)
# Send HIGH to switch on LED - this turns on
the LED
time.sleep(1) - this creates a pause
digitalWrite(led, 0)
# Send LOW to switch off LED - this
switches off the LED
time.sleep(1) - this creates a pause
except KeyboardInterrupt: #Turn LED off before stopping - if the following is
true, it means that the Pi's previous instructions will be overridden and this will
happen
digitalWrite(led, 0) - this turns off the LED
break
except 10Error :
# Print "Error" if communication error
encountered - if there is an ERROR or problem, it will just come up with 'Error'
print "Error"
NB: The text after the dash and in the purple highlight is the annotation for the
code does and is NOT part of the code.

Light Sensor
This is the code for the light sensor:
# GrovePi + Grove Light Sensor + LED - these are comments for the coder's use
# http://www.seeedstudio.com/wiki/Grove_-_Light_Sensor - this is an info page online for the light sensor
# http://www.seeedstudio.com/wiki/Grove_-_LED_Socket_Kit - this is an info page online for the LED light
import time - this records the current time
import grovepi - this will import the pre-written software by grovepi
# Connect the Grove Light Sensor to analog port A0 - another comment for the coder
# SIG, NC, VCC, GND
led = 4 - this labels the LED as a value of 4

# Turn on LED once sensor exceeds threshold resistance - this means that if the light sensor detects light, it will start to record the
threshold = 10 - in this case, the threshold for recording is 10
grovepi.pinMode(light_sensor, "INPUT") - this means that what the light sensor records is the input or information
grove.pinMode(led, "OUTPUT") - this means that what the LED does is the result or output
while True: - this means that if the following is accurate, do these actions:

try:
# Get sensor value - another comment, symbolised by a hashtag
sensor_value = grovepi.analogRead(light_sensor) - this collects the value of the sensor

# Calculate resistance of sensor in K 0 comment


resistance = (float) (1023 - sensor_value) * 10 / sensor_value - this calculates the resistance of the se

if resistance > threshold: - if (~) is greater than (~), then do this:


# Send HIGH to switch on LED - comment
grovepi.digitalWrite(led, 1) - this switches on the LED if the resistance is higher than the threshol
else: - if the above is incorrect, then do the following:
# Send LOW to switch on LED
grovepi.digitalWrite(led, 0) - comment

print "sensor_value =", sensor_value, " resistance =", resistance - this creates labels for the informati
time.sleep(.5) - this creates a pause
except 10Error: - if there is an error in the system, this will be printed
print "Error"

NB: The text after the dash and in the purple highlight is the annotation for the code does and is NOT part o

Sound Sensor
This is the coding for the Sound sensor:

# GrovePi + Grove Sound Sensor + Grove LED - comments for what this sensor is
# http://www.seeedstudio.com/wiki/Grove_-_Sound_Sensor - this takes you to an online page about the sou
# http://www.seeedstudio.com/wiki/Grove_-_LED_Socket_Kit - this takes you to an online page about the L
import time - this records the current time
import grovepi - this imports written software from the grovePi
# Connect the Grove Sound Sensor to analog port A0 - a comment for the reader's use/knowledge
# SIG, NC, VCC, GND - another comment
sound_ sensor = 0 - this labels the value of the (sound_sensor) as 0
# Connect the Grove LED to digital port D5 - another comment
# SIG, NC, VCC, GND - a comment
led = 5 - this labels the value of the LED as 5

grovepi .pinMode(sound_sensor , "INPUT" ) - this labels the sound sensor as the input or source of informa
grovepi .pinMode(led , "OUTPUT") - this labels the LED as the source of output
# The threshold to turn the led on 400.00 * 5 / 1024 = 1.95v - a comment
threshold_value = 400 - this makes the threshold value 400

while True: - if the following is true, then do this:


try:
# Read the sound level - this is another comment
sensor_value = grovepi .analogRead(sound_sensor) - this will measure the level of sound the senso

# If loud, illuminate LED, otherwise dim


if sensor_value > threshold_value: - if the sound goes above the threshold, which means it is noisy
grovepi .digitalWrite(led,1)
else:
grovepi.digitalWrite(led,0) - if the room is quiet, however, the LED will remain off
print "sensor_value =" , sensor_value - this organizes the collected information on the screen
time.sleep(.5) - this creates a pause
except 10Error: - if there is an error, this will be printed
print "Error"

NB: The text after the dash and in the purple highlight is the annotation for the code does and is NOT part o

Temperature and Humidity Sensor


This is the coding for the Temperature and Humidity Sensor:

# GrovePi + Grove Temperature & Humidity Sensor Pro - a comment for the reader's use
# http://www.seeedstudio.com/wiki/Grove_-_Temperature_and_Humidity_Sensor_Pro - this is an online pa
import grovepi - this imports the relevant software to the Pi
# Connect the Grove Temperature & Humidity Sensor Pro to digital port D4 - a comment for the reader
# SIG, NC, VCC, GND - another comment, signified by a hashtag
sensor = 4 - this labels the sensor's value as 4

while True: - if the following is correct, this will happen:


try:
[temp,humidity] = grovepi .dht(sensor,0) - the Pi will record the temperature and humidity of the r
print "temp =", temp, " humidity =', humidity - this displays the info for the reader
except 10Error: - if there is a problem, it will be displayed by 'ERROR'
print "Error"

NB: The text after the dash and in the purple highlight is the annotation for the code does and is NOT part o

Vous aimerez peut-être aussi