Lesson 3.1.1 Counting Device

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp

Introduction

Here we will make a number-displaying counter system, consisting of a PIR sensor and a 4-digit segment display. When the PIR detects that someone is passing by, the number on the 4-digit segment display will add 1. You can use this counter to count the number of people walking through the passageway.

Components

Schematic Diagram

T-Board NamephysicalwiringPiBCM
GPIO17Pin 11017
GPIO27Pin 13227
GPIO22Pin 15322
SPIMOSIPin 191210
GPIO18Pin 12118
GPIO23Pin 16423
GPIO24Pin 18524
GPIO26Pin 372526

Experimental Procedures

Step 1: Build the circuit.

  • For C Language Users

Step 2: Go to the folder of the code.

    cd /home/pi/davinci-kit-for-raspberry-pi/c/3.1.1/

Step 3: Compile the code.

    gcc 3.1.1_CountingDevice.c -lwiringPi

Step 4: Run the executable file.

    sudo ./a.out

After the code runs, when the PIR detects that someone is passing by, the number on the 4-digit segment display will add 1.

Code Explanation

void display()
{
    clearDisplay();
    pickDigit(0);
    hc595_shift(number[counter % 10]);

    clearDisplay();
    pickDigit(1);
    hc595_shift(number[counter % 100 / 10]);

    clearDisplay();
    pickDigit(2);
    hc595_shift(number[counter % 1000 / 100]);
 
    clearDisplay();
    pickDigit(3);
    hc595_shift(number[counter % 10000 / 1000]);
}

First, start the fourth segment display, write the single-digit number. Then start the third segment display, and type in the tens digit; after that, start the second and the first segment display respectively, and write the hundreds and thousands digits respectively. Because the refreshing speed is very fast, we see a complete four-digit display.

void loop(){
    int currentState =0;
    int lastState=0;
    while(1){
        display();
        currentState=digitalRead(sensorPin);
        if((currentState==0)&&(lastState==1)){
            counter +=1;
        }
        lastState=currentState;
    }
}

This is the main function: display the number on the 4-digit segment display and read the PIR value. When the PIR detects that someone is passing by, the number on the 4-digit segment display will add 1.

  • For Python Language Users

Step 2: Go to the folder of the code. 

    cd /home/pi/davinci-kit-for-raspberry-pi/python/

Step 3: Run the executable file.

    sudo python3 3.1.1_CountingDevice.py

After the code runs, when the PIR detects that someone is passing by, the number on the 4-digit segment display will add 1.

Code Explanation

Based on 1.1.5 4-Digit 7-Segment Display, this lesson adds PIR module to change the automatic counting of lesson 1.1.5 into count detecting. When the PIR detects that someone is passing by, the number on the 4-digit segment display will add 1.

def display():
    global counter                    
    clearDisplay() 
    pickDigit(0)  
    hc595_shift(number[counter % 10])

    clearDisplay()
    pickDigit(1)
    hc595_shift(number[counter % 100//10])

    clearDisplay()
    pickDigit(2)
    hc595_shift(number[counter % 1000//100])

    clearDisplay()
    pickDigit(3)
    hc595_shift(number[counter % 10000//1000])

First, start the fourth segment display, write the single-digit number. Then start the third segment display, and type in the tens digit; after that, start the second and the first segment display respectively, and write the hundreds and thousands digits respectively. Because the refreshing speed is very fast, we see a complete four-digit display.

def loop():
global counter
    currentState = 0
    lastState = 0
    while True:
        display()
        currentState=GPIO.input(sensorPin)
        if (currentState == 0) and (lastState == 1):
            counter +=1
        lastState=currentState

This is the main function: display the number on the 4-digit segment display and read the PIR value. When the PIR detects that someone is passing by, the number on the 4-digit segment display will add 1.

Phenomenon Picture