STM32F4Discovery Tutorial 4 - Motion Sensor

We have learned about interrupts and types of interrupts available on the stm32f4discovery board. We have learned about interrupt priorities and also we have created a program also to see how interrupt and interrupt priorities actually work in reality. So In this tutorial we will create a very cool product called motion sensor detection. The product will detect human motion. And when it detects any motion it will respond with something.

Now if you go to internet and try to search for home security systems then you will find many products. And even I am quite sure that many of you guys have already a such kind of system in your home and you might be using those for security and surveillance. Now if you closely see the details of many of these systems you will find that one thing is common on most of the products. That is motion detection. These systems can detect motion so that in case of your absence you still will be able to know that someone is present. Or may be someone broke into your house or anything kind of.

A similar kind of approach we will do here but not very advanced but basic. Our product will detect motion and if it detects it will just light up a led. Hence you will find this to be a very interesting tutorial.

#include "stm32f4xx.h"

GPIO_InitTypeDef GPIO_InitLed;
EXTI_InitTypeDef EXTI_InitSensor;
NVIC_InitTypeDef NVIC_InitSensor;

void Delay(__IO uint32_t nCount){

int main(void)
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

	GPIO_InitLed.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
	GPIO_InitLed.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitLed.GPIO_OType = GPIO_OType_PP;
	GPIO_InitLed.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOD, &GPIO_InitLed);

	GPIO_InitLed.GPIO_Pin = GPIO_Pin_1;
	GPIO_InitLed.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitLed.GPIO_Mode = GPIO_OType_PP;
	GPIO_InitLed.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitLed);

	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource1);

	EXTI_InitSensor.EXTI_Line = EXTI_Line1;
	EXTI_InitSensor.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitSensor.EXTI_Trigger = EXTI_Trigger_Rising;
	EXTI_InitSensor.EXTI_LineCmd = ENABLE;


	NVIC_InitSensor.NVIC_IRQChannel = EXTI1_IRQn;
	NVIC_InitSensor.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitSensor.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitSensor.NVIC_IRQChannelCmd = ENABLE;

	while (1)
		GPIO_WriteBit(GPIOD, GPIO_Pin_0, Bit_SET);
		GPIO_WriteBit(GPIOD, GPIO_Pin_0, Bit_RESET);

void EXTI1_IRQHandler(void)
	if(EXTI_GetITStatus(EXTI_Line1) != RESET)
		GPIO_WriteBit(GPIOD, GPIO_Pin_1, Bit_SET);
		GPIO_WriteBit(GPIOD, GPIO_Pin_1, Bit_RESET);
Close Menu