You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 12 Next »

Bootup GPIO States

Pin@startup
Pin@startup
3v3-
5v-
GPIO 2HIGH
5v-
GPIO 3HIGH
GND-
GPIO 4HIGH
GPIO 14 (TxD)-
GND-
GPIO 15 (RxD)-
GPIO 17LOW
GPIO 18LOW
GPIO 27LOW
GND-
GPIO 22LOW
GPIO 23LOW
3v3-
GPIO 24LOW
GPIO 10(MOSI)LOW
GND-
GPIO 9(MISO)LOW
GPIO 25LOW
GPIO 11(SCLK)LOW
GPIO 8~0.5v
GND-
GPIO 7~0.5v
ID_SD-
ID_SC-
GPIO 5HIGH
GND-
GPIO 6HIGH
GPIO 12LOW
GPIO 13LOW
GND -
GPIO 19LOW
GPIO 16LOW
GPIO 26LOW
GPIO 20LOW
GND-
GPIO 21LOW

Python Sample Code

> vi sample.py

sample.py
# External module imports
import RPi.GPIO as GPIO
import time

# Pin Definitons:
PIN_TRUNK_SENSOR = 27 
PIN_DOOR_SENSOR  = 6
PIN_DOOR_1 	   = 12
PIN_DOOR_2 	   = 25
PIN_TRUNK_1 	   = 22
PIN_TRUNK_2      = 13

# Pin Setup:
GPIO.setmode(GPIO.BCM) 
GPIO.setup(PIN_TRUNK_SENSOR, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 
GPIO.setup(PIN_DOOR_SENSOR, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)  

GPIO.setup(PIN_DOOR_1, GPIO.OUT) 
GPIO.setup(PIN_DOOR_2, GPIO.OUT) 
GPIO.setup(PIN_TRUNK_1, GPIO.OUT) 
GPIO.setup(PIN_TRUNK_2, GPIO.OUT) 

# Initial state
GPIO.output(PIN_DOOR_1, GPIO.LOW)
GPIO.output(PIN_DOOR_2, GPIO.LOW)
GPIO.output(PIN_TRUNK_1, GPIO.LOW)
GPIO.output(PIN_TRUNK_2, GPIO.LOW)

doorSensorVal=False
trunkSensorVal=False

newDoorSensorVal=False
newTrunkSensorVal=False

print("Here we go! Press CTRL+C to exit")
try:
    while 1:
		
		#read DOOR sensors
		newDoorSensorVal=GPIO.input(PIN_DOOR_SENSOR)	
		if (newDoorSensorVal != doorSensorVal):
			doorSensorVal=newDoorSensorVal	
			if(doorSensorVal==True):
				#lock
				print(">DOOR SENSOR = LOW (LOCK)")
				GPIO.output(PIN_DOOR_1, GPIO.HIGH)
				GPIO.output(PIN_DOOR_2, GPIO.LOW)
				time.sleep(0.075)
				GPIO.output(PIN_DOOR_1, GPIO.LOW)
				GPIO.output(PIN_DOOR_2, GPIO.LOW)
			else:
				print(">DOOR SENSOR = HIGH (UNLOCK)")
				GPIO.output(PIN_DOOR_1, GPIO.LOW)
				GPIO.output(PIN_DOOR_2, GPIO.HIGH)
				time.sleep(0.075)
				GPIO.output(PIN_DOOR_1, GPIO.LOW)
				GPIO.output(PIN_DOOR_2, GPIO.LOW)

		#read TRUNK sensors
		newTrunkSensorVal=GPIO.input(PIN_TRUNK_SENSOR)	
		if (newTrunkSensorVal != trunkSensorVal):
			trunkSensorVal=newTrunkSensorVal	
			if(trunkSensorVal==True):
				#lock
				print(">TRUNK SENSOR = LOW (LOCK)")
				GPIO.output(PIN_TRUNK_1, GPIO.HIGH)
				GPIO.output(PIN_TRUNK_2, GPIO.LOW)
				time.sleep(0.075)

				GPIO.output(PIN_TRUNK_1, GPIO.LOW)
				GPIO.output(PIN_TRUNK_2, GPIO.LOW)
			else:
				print(">DOOR SENSOR = HIGH (UNLOCK)")
				GPIO.output(PIN_TRUNK_1, GPIO.LOW)
				GPIO.output(PIN_TRUNK_2, GPIO.HIGH)
				time.sleep(0.075)
				GPIO.output(PIN_TRUNK_1, GPIO.LOW)
				GPIO.output(PIN_TRUNK_2, GPIO.LOW)

except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
    GPIO.cleanup() # cleanup all GPIO


Run the program

> sudo python sample.py


NodeJS Sample Code

The onoff library has show to be buggy! Use gpio library instead.

Sample using "onoff" node js library
var Gpio = require('onoff').Gpio; //include onoff to interact with the GPIO

var LED = new Gpio(4, 'out'); //use GPIO pin 4, and specify that it is output
var blinkInterval = setInterval(blinkLED, 250); //run the blinkLED function every 250ms

function blinkLED() { //function to start blinking
  if (LED.readSync() === 0) { //check the pin state, if the state is 0 (or off)
    LED.writeSync(1); //set pin state to 1 (turn LED on)
  } else {
    LED.writeSync(0); //set pin state to 0 (turn LED off)
  }
}

function endBlink() { //function to stop blinking
  clearInterval(blinkInterval); // Stop blink intervals
  LED.writeSync(0); // Turn LED off
  LED.unexport(); // Unexport GPIO to free resources
}

setTimeout(endBlink, 5000); //stop blinking after 5 seconds


References

  • No labels