[Subscribe to the Recursive Labs email Newsletter to get updates][Find out more about our online courses] [Connect with us on Twitter][Facebook Page]
Jun 7, 2012
It is very easy to interface a Parallax PIR motion sensor to the STM32L-DISCOVERY board using ChibiOS/RT.
The sensor works with 3v, so we can use the 3v output from the discovery board to power it. Simply connect the output pin of the sensor to an I/O pin of the STM32 processor (I am using PA11). The sensor outputs a HIGH for a few seconds whenever it detects motion - this can be easily detected by reading from the I/O pin.
Here is a small program which lights up the blue LED on the discovery board whenever the sensor detects movement.
#include "ch.h"
#include "hal.h"
#define PIR 11
int main(void) {
halInit();
chSysInit();
palSetPadMode(GPIOB, GPIOB_LED4, PAL_MODE_OUTPUT_PUSHPULL);
palSetPadMode(GPIOA, PIR, PAL_MODE_INPUT);
while (TRUE) {
if(palReadPad(GPIOA, PIR))
palSetPad(GPIOB, GPIOB_LED4);
else
palClearPad(GPIOB, GPIOB_LED4);
chThdSleepMilliseconds(10);
}
}