Getting Data from Sensor – Part 2 of 3

Raspberry Pi can work with “digital” sensors. To work with analaog sensors, it will require an ADC e.g. MCU 3008. This article uses a Passive Infra Red (PIR) sensor to detect if someone has been near the sensor.
Getting temperature and humidity from sensors requires some tweaking. I will cover it in another article.

At the end of this part, your Pi should be able to detect human presence using sensor and print it on the screen.

Additional Parts Required

  • PIR Sensor
  • Female to Female connector cables

Connect as follows:

  • 5V power to the +ve of the PIR Sensor
  • GND of Raspberry Pi to the ground of the sensor
  • GPIO 14 of Raspberry Pi to the center pin of the sensor

Here is the pinout diagram for Raspberry Pi(2,3,4)

gpio-pinout-diagram-2

Here is the sketch of diagram

RaspberryPiPIR_bb

Lets get coding.

It may be hard to code directly on Raspberry Pi using nano editor. I personally use VS Code editor to create the file locally on the machine and send it to the Raspberry Pi using WinSCP. (There is also an “Remote – SSH” extension for VS code. That allows you to edit files located on Raspberry Pi. But currently the extension is in preview mode. So it may not be rock solid. I will stick to WinSCP for now.)
Also from VS Code, one can open powershell and SSH into the terminal. So you many not need monitor or the HDMI cable.

This will perform initial setup and then be in loop to detect, if the PIR sensor has detected any person.

You can download the file here.

# import for async
import asyncio

# import library for GPIO
import RPi.GPIO as GPIO

# time library
import time

# Port of PIR i.e. GPIO14
pir_port = 14

# function to init GPIO of the Pi
def InitGPIO():
    # print GPIO info (not needed for initialization. Just to ensure that it works)
    print(GPIO.RPI_INFO)

    # Setup GPIO mode to Broadcom
    GPIO.setmode(GPIO.BCM)

    # configure GPIO14 as input pin
    GPIO.setup(pir_port,  GPIO.IN)

async def main():
    # stay in the loop to monitor PIR
    try:
        while (True):
            # if the input is zero, nobody is there at the sensor
            if GPIO.input(pir_port) == 0:
                print("000")
            else:
                # found somebody at the sensor
                print("111")
            # wait for 1 sec whether you detect someone or not
            time.sleep(1)
    # If user presses ^C cleanup the GPIO and disconnect from the IoT Hub
    except KeyboardInterrupt:
        GPIO.cleanup()
    print("Exiting")

if __name__ == "__main__":
    InitGPIO()
    asyncio.run(main())

Transfer the file to the Raspberry Pi using WinSCP.

To run the program, ssh into Raspberry Pi. It can be done as follows:

  • In VS Code, open terminal
  • Get the IP address of Raspberry Pi using command

ping raspberrypi.local

  • You can ssh in it as follows

ssh pi@<ipAddress>

  • The default password for Raspberry pi is “raspberry”
  • Navigate to the folder where you copied the Python file and run the following command to run the program

python3 <filename>

It should show output equivalent to the following. 000 is printed when there is nothing in front of the sensor and 111 means the sensor has detected human (or part of human) in front of it.

runningSensorOnly

Running this successfully means your program and the hardware setup can detect when human is present in front of the sensor. You are ready to send the data to Azure.

When the program is stopped using Ctrl+C, please make sure that GPIO clean up is performed.

 

<Previous – Preparing Raspberry Pi                  Next  – Sending the data to Azure>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s