Saturday, August 13, 2016

PIR sensor and STM32F4 Discovery Board

Today I'm gonna show you how to detect a motion with the PIR sensor connected to the STM32F4 Discovery Board.

What are PIR sensors?
The acronym PIR means Passive Infrared. Those sensors are capable of detecting a motion mostly caused by humans. They are small and inexpensive and therefore commonly used in homes or businesses.

Things you need:
  • STM32F4 Discovery Board with the USB cable
  • PIR Sensor
  • 3 female/female jumper wires or soldering skill

1. Wiring:

STM32F4 PIR Sensor Cable Color
5V Power Red
PD5 Signal Yellow
GND Ground Black

2. Code:

We'll use STM32F4 standard GPIO library. The full code is available here. I'll break it in steps.

a) LEDs initalization

With the help of the documentation we can see that LEDs are connected to the following pins: PD12 (green), PD13 (orange), PD14 (red) and PD15 (blue).

First we enable GPIOD clock.
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
Then we configure all LEDs in output pushpull mode.
GPIO_InitTypeDef led; led.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; led.GPIO_Mode = GPIO_Mode_OUT; led.GPIO_OType = GPIO_OType_PP; led.GPIO_Speed = GPIO_Speed_2MHz; led.GPIO_PuPd = GPIO_PuPd_NOPULL;
Note: Instead of GPIO_Pin_12, GPIO_Pin_13, ... you can also use GPIO_Pin_All to apply same settings to all pins.

And now initialize the whole structure.
GPIO_Init(GPIOD, &led);
b) Sensor pin initialization

The PIR sensor is connected to PD5 pin. We'll configure the settings in input pushpull mode.
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitTypeDef pin; pin.GPIO_Pin = GPIO_Pin_5; pin.GPIO_Mode = GPIO_Mode_IN; pin.GPIO_Speed = GPIO_Speed_2MHz; pin.GPIO_OType = GPIO_OType_PP; pin.GPIO_PuPd = GPIO_PuPd_UP; 
GPIO_Init(GPIOD, &pin);
c) Main program

Check in the loop whether the sensor detected the motion and turn on the LEDs in that case.
while (1) {
    if (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_5)) {
      GPIO_SetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
    } else {
      GPIO_ResetBits(GPIOD, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
    }
  }
Instead of using LEDs on the STM board you can connect the external one (see the previous tutorial). As a fun addition to the project you can also add the sound. 


Wednesday, August 10, 2016

Controlling the external LED with the STM32F4 Discovery Board


In this short tutorial I will show you how to toggle the external LED with the STM's built-in user button (the blue one in the picture above). Although it's very simple, it shows you some basics of controlling GPIO pins.

Things you need:
  • STM32F4 Discovery Board with the USB cable
  • Breadboard
  • Standard LED
  • Resistor (at least 1k Ohm)
  • 2 female/male jumper wires
  • 1 male/male jumper wire

1. Setting up the circuit

STM32F4 Breadboard Cable Color
PD5 Positive power rail (red) Orange
GND Negative power rail (blue) White

Connect the LED in series with the resistor and complete the circuit with the jumper wire (in my case green).

Note: Since all LEDs are polarized, the longer leg of the LED (the positive, anode pin) should be on the resistor's side (the right leg in the picture below).



2. Writing the code

The simplest way is to use STM32F4 standard GPIO library. The full code is available here. I will explain it step by step.

a) LED

Since our LED is connected to PD5 you have to enable GPIOD clock. 
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
Next configure the PD5 pin in output pushpull mode.
GPIO_InitTypeDef led;
led.GPIO_Pin = GPIO_Pin_5;
led.GPIO_Mode = GPIO_Mode_OUT;
led.GPIO_OType = GPIO_OType_PP;
led.GPIO_Speed = GPIO_Speed_2MHz;
led.GPIO_PuPd = GPIO_PuPd_NOPULL;
And now initialize the structure.
GPIO_Init(GPIOD, &led);
 b) User button

The blue user button is connected with PA0. Let's configure it in input pushpull mode with the pull-up.
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

GPIO_InitTypeDef b;
b.GPIO_Mode = GPIO_Mode_IN;
b.GPIO_Speed = GPIO_Speed_2MHz;
b.GPIO_OType = GPIO_OType_PP;
b.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOA, &b);
c) Main program

The simplest part. Check in loop whether the button was pressed and if so, toggle the LED. 
while (1) {
    if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) {
      GPIO_ToggleBits(GPIOD, GPIO_Pin_5);
      delay();
    }
  }
Note: The button is pretty sensitive on click so we added a delay to prevent the LED from blinking too much.


Tuesday, September 29, 2015

Original friction plate

This is how our first prototype of the simple friction drum looked like:


It's a copper plate divided into five rectangular shapes, each one connected to MPR121 Capacitive Touch Sensor and then to Raspberry Pi.


Monday, September 21, 2015

The original friction drum in action

Traditional vs. American way of playing a friction drum.



We showed this video at Music Tech Fest and the audience found it quite funny I think. :)


Sunday, September 20, 2015

New sounds

Strange Sounds:



Ode of Joy:



Fun:




Tuesday, September 15, 2015

All the friction 'bout that drum

This summer I've been given a nice opportunity to work on interesting musical project at my school's Laboratory of Computer Graphics and Multimedia. You probably heard or at least saw this instrument once in your life:

It's a friction drum, an instrument used most commonly in folk music. It consists of a membrane stretched over a sound box and the sound comes from vibration that is achieved by friction.

Here you can listen to the sound of friction bass I recorded.



Laboratory's idea was to create a similar but electrical instrument using cheap and simple electronics.



And this is the result!

I'm gonna post more project's details in the next days. Stay tuned. :)