3.1.9 Alarm Bell


In this project, we will make a manual alarm device. You can replace the toggle switch with a thermistor or a photosensitive sensor to make a temperature alarm or a light alarm.

Schematic Diagram

T-Board Name





Pin 11




Pin 12




Pin 13




Pin 15




Experimental Procedures

Step 1: Build the circuit.


Step 2: Change directory.

cd /home/pi/raphael-kit/c/3.1.9/

Step 3: Compile.

gcc 3.1.9_AlarmBell.c -lwiringPi -lpthread

Step 4: Run.

sudo ./a.out

After the program starts, put the slide switch to the right, and the buzzer will give out alarm sounds. At the same time, the red and green LEDs will flash at a certain frequency.


If it does not work after running, or there is an error prompt: “wiringPi.h: No such file or directory”, please refer to C code is not working?.

Code Explanation

#include <pthread.h>

In this code, you’ll use a new library, pthread.h, which is a set of common thread libraries and can realize multithreading. We add the -lpthread parameter at compile time for the independent working of the LED and the buzzer.

void *ledWork(void *arg){

The function ledWork() helps to set the working state of these 2 LEDs: it keeps the green LED lighting up for 0.5s and then turns off; similarly, keeps the red LED lighting up for 0.5s and then turns off.

void *buzzWork(void *arg){
            pitch = -pitch;

The function buzzWork() is used to set the working state of the buzzer. Here we set the frequency as between 130 and 800, to accumulate or decay at an interval of 20.

void on(){
    flag = 1;
    if(softToneCreate(BeepPin) == -1){
        printf("setup softTone failed !");
    pthread_t tLed;
    pthread_t tBuzz;

In the function on():

  1. Define the mark flag=1, indicating the ending of the control thread.

  2. Create a software-controlled tone pin BeepPin.

  3. Create two separate threads so that the LED and the buzzer can work at the same time.

  • pthread_t tLed: Declare a thread tLed.

  • pthread_create(&tLed,NULL,ledWork,NULL): Create the thread and its prototype is as follows:

int pthread_create(pthread_t *restrict tidp,const pthread_attr_t*restrict_attr,void*(*start_rtn)(void*),void *restrict arg);

If successful, return 0 ;otherwise, return the fall number -1.

  • The first parameter is a pointer to the thread identifier.

  • The second one is used to set the thread attribute.

  • The third one is the starting address of the thread running function.

  • The last one is the one that runs the function.

void off(){
    flag = 0;

The function Off() defines “flag=0” so as to exit the threads ledWork and BuzzWork and then turn off the buzzer and the LED.

int main(){
    int lastState = 0;
        int currentState = digitalRead(switchPin);
        if ((currentState == 1)&&(lastState==0)){
        else if((currentState == 0)&&(lastState==1)){
    return 0;

Main() contains the whole process of the program: firstly read the value of the slide switch; if the toggle switch is toggled to the right (the reading is 1), the function on() is called, the buzzer is driven to emit sounds and the the red and the green LEDs blink. Otherwise, the buzzer and the LED don’t work.

Phenomenon Picture