This week objective is to read and use signal coming from a sensor.
Sensor a device which detects or measures a physical property and records, indicates, or otherwise responds to it.
I decided that I'll work on the Pyroelectric sensor which detects motion,Because it one of the things I'll use in my final project.
PIR Sensor: The PIR sensor stands for Passive Infrared sensor. It is a low cost sensor which can detect the presence of Human beings or animals. I used the module provided on the academy website labeled (HC-sr501).
Pin Number |
Pin Name |
Description |
1 |
Vcc |
Input voltage is +5V for typical applications. Can range from 4.5V- 12V |
2 |
High/Low Ouput (Dout) |
Digital pulse high (3.3V) when triggered (motion detected) digital low(0V) when idle(no motion detected) |
3 |
Ground |
Connected to ground of circuit |
The PIR sensor stands for Passive Infrared sensor. It is a low cost sensor which can detect the presence of Human beings or animals. This sensor has three output pins Vcc, Output and Ground as shown in the pin diagram above. Since the output pin is 3.3V TTL logic it can be used with any platforms like Arduino, Raspberry, PIC, ARM, 8051 etc..
The module can be powered from voltage 4.5V to 20V but, typically 5V is used. Once the module is powered allow the module to calibrate itself for few minutes, 2 minutes is a well settled time. Then observe the output on the output pin. Before we analyse the output we need to know that there are two operating modes in this sensor such as Repeatable(H) and Non- Repeatable(L) and mode. The Repeatable mode is the default mode.
The output of the sensor can be set by shorting any two pins on the left of the module as shown below. You can also notice two orange colour potentiometers that can be used to set the sensitivity and time which will be explained further below.
In Repeatable(H) mode the output pin Dout will go high (3.3V) when a person is detected within range and goes low after a particular time (time is set by “Off time control” potentiometer). In this mode the output pin will go high irrespective of whether the person is still present inside the range or has left the area. The sensitivity can be set using the “sensitivity control” potentiometer.
In “I” mode the output pin Dout will go high (3.3V) when a person is detected within range and will stay high as long as he/she stays within the limit of the Sensors range. Once the person has left the area the pin will go low after the particular time which can be set using the potentiometer. The sensitivity can be set using the “sensitivity control” potentiometer.
There are two important materials present in the sensor one is the pyroelectric crystal which can detect the heat signatures from a living organism (humans/animals) and the other is a Fresnel lenses which can widen the range of the sensor. Yes the white colour things is just a lense that is used to widen the range of the sensor, if you remove the lense you can find the Pyroelectric sensor inside it covered inside a protective metal casing as shown above.
I used Eagle software like usual to design my PCB. and I did exactly what I did in Electronics production week that you are kindly asked to refer to it.
Because of the module size I had to bend the ping headers a bit to make them touch the PCB as shown below.
This design I connected the LED in parallel which acted as a voltage divider circuit that led to abnormal respones because the voltage was way below desired to detect it as a HIGH signal.
As shown below again the pins of the PIR sensor (Make sure the orientation is correct and the orange potentiometer at the bottom and the pins are at the top)
and then connect them to the board:
The white labeled pins will be connected to the sensor corresponding pins.
And the red labeled pins will be connected to the FTDI corresponding pins.
The IR sensor connections are almost the same what only changed was the sequence of pins on the IR sensor not the board and the enable pin that I didn't use.
With green enable cap plugged as shown below, EN terminal is equal to "1" when the sensor does not work, equal to "0" when working, which I found it with no use in my particular example, yet it could be used to check if the sensor is working or not, but signal pin is used for knowing whether there's a reading or not which's the desired.
The white labeled pins will be connected to the sensor corresponding pins.
And the red labeled pins will be connected to the FTDI corresponding pins.
Lastly, if you're confused between what's Rx a what's Tx, I advice you to visit Networks and Communication week to understand more.
I used the help of this excellent reference to understand how to initialize serial communication between my board and PC.
// This is a comment that doesn't affect the code, just text to explain. #include <SoftwareSerial.h> //This is a library that make us initialize the communication to use the TX & RX pin. #define RX 3 // In my board I don't use the RX, I chose an unconnected pin. #define TX 2 // the pin that the ATtiny RX will send data to the PC SoftwareSerial Serial(RX, TX); // giving the pin variables to the softwareSerial library after defining them in the line upward. int val=0; //variable to store the values of the reading. void setup() { Serial.begin(9600); //define the baud rate that the communication will start on. } void loop() { val = digitalRead(2); // read the input pin and store it in Val variable. delay(250); //Optional delay to ease reading the serial monitor values. Serial.println(val); // print the values stored in Val variable. }
Before uploading make sure you've selected the right settings for your ATTiny45:
If you can't find a COM for your board:
First, make sure your FTDI cable is connected correctly as well as your other board cable either it's usb or tinyUsb.
Secondly, make sure you've installed the proper drivers for your FTDI cable (Check Embedded Programming academy tutorials)
As you can see I changed the sensor at the end because the (HC-SR501) was detecting motion with a big delay and have to wait between the readings which's not the purpose I wanted to have in particular, so I used IR Infrared sensor to detect motion.
Using the connection I instructed you to use above in the IR and PIR connection part, you just change the wires connection and the same arduino code uploaded will work. How ?
Because both sensor send similar data:
PIR: Sends either 0 for detecing or 1 for detecting nothing.
IR: Sends either 0 for detecing or 1 for detecting nothing.