Orange Raspberry Pi 4/Zero Beginner Kit
We all wanted to start working with the Raspberry Pi board, but due to a lack of knowledge and direction, we were unable to make the decision of which path to take.
We understand your issue, which is why we came up with the idea of creating Raspberry Pi board kits. Please take a look at the other Raspberry Pi kits that are available.
IDE and Programming Language For Raspberry Pi Board
Because we are working on the Raspberry Pi board, we will need a programming language and an IDE to write the language that will communicate with the peripherals that we will connect to the IDE.
So, because Python is a popular and easy-to-use programming language, we will use Python and the Thorny IDE to run our Python program.
When it comes to installing the IDE and programming language, there is no need to install any additional software on the system. The Thorny IDE and the Python interpreter are already installed on the system.
So, this was all about the software.
LED ( Light Emitting Diode)
We’ve all heard of the light-emitting diode. When the required operating voltage is applied to these diodes, they emit light, as the name implies.
It has two terminals, one for the anode and one for the cathode. To turn on the LED, connect these terminals to the VCC and GND pins of the power supply.
If you want to know more about the LED then please refer to the booklet. We have shared all the required information about the LED in the booklet.
LED Interfacing With The Raspberry Pi 4
Now that we understand the fundamentals of the LED, we can begin interfacing it with the Raspberry Pi.
In the image below, we used a 320ohm resistor to connect the LED to the Raspberry Pi. The resistor is used to reduce the operating voltage of the LED.
The raspberry pi generates a 3.3v output on its GPIO pin, which is slightly higher than the LED’s operating voltage. To prevent the LED from being damaged by overvoltage, we used a 320ohm resistor to connect it to the raspberry pi.
The Following image shows the interfacing diagram of the LED with the Raspberry Pi.
You can use the following python code to work with the raspberry pi.
import RPi.GPIO as GPIO import time LED_PIN = 17 GPIO.setmode(GPIO.BCM) GPIO.setup(LED_PIN, GPIO.OUT) GPIO.output(LED_PIN, GPIO.HIGH) time.sleep(1) GPIO.output(LED_PIN, GPIO.LOW) GPIO.cleanup()
In the preceding code, we used two Python libraries. The first is a time library, and the second is an RPI (Raspberry Pi) library.
Now, What is Python Library?
Unlike the Arduino and C programming languages, libraries are also available in Python. However, In python, they are referred as modules.
Python modules are simply collections of functions and classes.
These modules help us to simplify and make the code easier to read.
If the modules were not available, we had to proceed with plain Python code. This could have made embedded programming more difficult.
What is The Function in Python?
Python functions are also known as Methods. In contrast to modules, functions in the Python programming language are a collection of Python statements.
Functions, according to my definition, are collections of statements that are created to reduce the repeatability of the same code.
We can also pass values to these functions depending on the circumstances. Or sometime functions can be executed without passing any values.
Similarly, Python functions can either return or not return the output.
In the following sections, we will create and use a plethora of functions. So, if you have any questions, please leave them in the comments section.
def function(input1, input2): print(“Adding input1 and input2”) # Print the message which in double quotes print(input1 + input2) # Print the addition of the inputs return input1 + input2 # return the output of the function function(2, 4);
Function is the name of the function that we created in the preceding code. This function accepts two arguments. Input 1 and Input 2
This function’s primary function is to add input parameters and print them on the serial monitor.
So, that was the function; if you have any questions about the functions, please leave them in the comments section.
Classes in Python
Talking about classes in python, Python classes are a collection of multiple methods. Python is both a functional and an OOPS programming language.
The difference between functional programming and oops programming is that functional programming is useful in situations where the state does not change.
The programming that we do with classes is known as imperative programming, and the programming that we do with functions is known as declarative programming.
Talking about the syntax and creating objects of the python classes, As stated below, we create the class object in order to call and execute the functions contained within the class.
Please take a look at the following example to clear up any confusion.
class Person: def __init__(self, name, age): self.name = name self.age = age p1 = Person("John", 36) print(p1.name) print(p1.age)
We’ll be using classes a lot in our code. In the preceding code, we created the person class, which accepts two parameters. One is the name, and the other is the age.
The object of the class is p1, and we pass the name and age parameters to the Person class.
After passing the parameters to the class, the initializer method will be called instantly and after the completion of the Initialization process, we will be able to access all of the variables and methods in the person class.
So, this was all about class.
If you have any questions, please leave them in the comments section.
Python LED Code Explanation:
import RPi.GPIO as GPIO import time LED_PIN = 17 GPIO.setmode(GPIO.BCM) GPIO.setup(LED_PIN, GPIO.OUT) GPIO.output(LED_PIN, GPIO.HIGH) time.sleep(1) GPIO.output(LED_PIN, GPIO.LOW) GPIO.cleanup()
In the above code, we have used the RPI library and the time library. The RPI library is specially designed for the raspberry pi board. Using this library, we can control the GPIO pins of the raspberry.
We used the RPI library and the time library in the preceding code. The RPI library was created specifically for the Raspberry Pi board. Using this library, we can control the Raspberry Pi’s GPIO pins.
The time library is the second library that we have used in this section. In the above code, we are using the sleep function. That function will halt the execution of the timer for the amount of time specified.
Following that, we set the pin’s mode and define the GPIO pin’s function.
So that was the GPIO.Mode method; now let’s look at the GPIO.setup() method.
This method is used to set the GPIO mode.
If the pin is an input, we can use GPIO.input; if the mode is output, we can use GPIO.output.
In our case, we want to generate output on a specific pin, so we used the GPIO.output function.
This was a discussion of the GPIO.output function.
Following that, we used GPIO.cleanup();
This GPIO.cleaup function is used to clear the data registers of the GPIO pin.
This was all about the code. Now as you know about the python code, you can use the following code and can run it on the raspberry pi.
So, this was about the raspberry pi python code, in the coming part of the video we will talk about the interfacing of the Button with the Raspberry pi.
Everyone knows about the Button; in this section you will learn about the type of Button available in the market.
These Buttons work just like normal Buttons. The only difference is that the lever of this Button is horizontal. In addition, this Button has a spring and locking mechanism.
When you press this Button, the spring get compressed, and the locking mechanism locks the Button. This is how the connection happens.
After that, when you press the Button again, the locking mechanism unlocks the Button, and the connection breaks and the Button acts as an off Button.
This type of Button is the same Button used in home appliances. The entire assembly of this Button is enclosed in the box, and a spring attachment button is placed above it.
The remaining function is similar to other types of Buttons. When you press it, it attaches to the metal contacts, and the connection occurs, and when you press it again, it loses contact with the metal contacts and returns to its initial position.
Interfacing The Button With The Raspberry Pi
The button that comes with this kit is a push-pull type. It has two pins, and connecting the Button to the Raspberry Pi is a breeze.
Even though it is a simple task, you must be concerned about the LED’s interfacing with the Raspberry Pi. The reason I’m asking you to be concerned is that, unlike the Arduino board, the Raspberry Pi board cannot handle voltages greater than 3.3V.
And if you apply a voltage greater than 3.3V, the raspberry pi will be damaged because the GPIO pins of the raspberry pi will not be able to handle the applied voltage.
As a result, you’ll need to connect a resistor between the GPIO pin and the Button to protect the Raspberry Pi. Something like what is depicted in the image below.
Raspberry Pi Python Script For Button
In this section of the blog, we will discuss the Python script that we can use to interact with the button.
We are using the same modules and functions as discussed in the LED coding section.
In the LED python script, we set the pin as an output pin, but in the case of the button, we must change the mode of the pin to an input pin because here in this case, we are reading the output of the pin.
The following code can be used to read the button’s output. Please check the following code and let me know if you face any issue.
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)#Button to GPIO23 GPIO.setup(24, GPIO.OUT) #LED to GPIO24 try: while True: button_state = GPIO.input(23) if button_state == False: GPIO.output(24, True) print('Button Pressed!!!') time.sleep(0.2) else: GPIO.output(24, False) except: GPIO.cleanup()
So, this is it for button interfacing with the raspberry pi. If you have any doubts then please let us know in the comment section.
How To Use Seven Segment Display With The Raspberry Pi?
The seven-segment display is used in a variety of applications. In this section of the blog, we will discuss how to connect the seven-segment display to the Raspberry Pi.
A 7-segment display is nothing more than a collection of seven LEDs linked together, with each LED referred to as a segment. Each of them can be controlled independently.
7-segment displays come in a variety of colours (Red, Blue, and Green) and sizes (0.56 to 6.5 inches). Two to four 7-segment displays may be stacked together to form a large display (refer to the following image).
Seven Segment Display Interfacing With The Raspberry Pi
The following image shows the interfacing of the seven-segment display with the raspberry pi.
Please do the connection as shown in the above image and let me know if you have any doubts.
Python Code For Seven-Segment Display
The following python code you can use to work with the raspberry pi.
import RPi.GPIO as GPIO import time import os, sys GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) #setup output pins GPIO.setup(10, GPIO.OUT) //GPIO19 GPIO.setup(12, GPIO.OUT) //GPIO18 GPIO.setup(13, GPIO.OUT) //GPIO16 GPIO.setup(15, GPIO.OUT) //GPIO13 GPIO.setup(31, GPIO.OUT) //GPIO12 GPIO.setup(33, GPIO.OUT) //GPIO20 GPIO.setup(38, GPIO.OUT) //GPIO21 GPIO.setup(40, GPIO.OUT) #define 7 segment digits # If your Display is not displaying the correct number on the display then you can do the changes to the following arrays(Lists). digitclr=[1,1,1,1,1,1,1] digit0=[0,0,0,0,0,0,1] digit1=[1,0,0,1,1,1,1] digit2=[0,0,1,0,0,1,0] digit3=[0,0,0,0,1,1,0] digit4=[1,0,0,1,1,0,0] digit5=[0,1,0,0,1,0,0] digit6=[0,1,0,0,0,0,0,] digit7=[0,0,0,1,1,1,1] digit8=[0,0,0,0,0,0,0] digit9=[0,0,0,1,1,0,0,] gpin=[10,12,13,15] #routine to clear and then write to display def digdisp(digit): for x in range (0,8): GPIO.output(gpin[x], digitclr[x]) for x in range (0,8): GPIO.output(gpin[x], digit[x]) #routine to display digit from 0 to 9 digdisp(digit0) time.sleep(1) digdisp(digit1) time.sleep(1) digdisp(digit2) time.sleep(1) digdisp(digit3) time.sleep(1) digdisp(digit4) time.sleep(1) digdisp(digit5) time.sleep(1) digdisp(digit6) time.sleep(1) digdisp(digit7) time.sleep(1) digdisp(digit8) time.sleep(1) digdisp(digit9) time.sleep(1) #tidy up GPIO.cleanup()
We haven’t included any libraries in this Python code; instead, we’ve written a few lines that do the work.
We initialised the pins at the beginning of the code. This is similar to what we did in the first stage of the code.
Following that, we initialised the digits. In this case, we’ve created a total of nine such variables, each of which has seven digits.
Following that, we used the digdisp function, and we are passing a digit as a value to it.
This function is in charge of displaying the numbers on the screen. Two FOR loops were used in this function. These loops toggle the segments of the seven segment displays on and off.
Following this function, we added the python code to enable the individual segments of the seven segments display.
This was all about the code. This code can now be run on the Raspberry Pi. If you have any problems, please let me know in the comments section.
In the following section of the blog, we will discuss how to connect the Active Buzzer to the Raspberry Pi.
Buzzer With The Raspberry Pi
Buzzers are found in a variety of instruments. It serves as an indicator in a variety of systems, including home appliances, alarm systems, and electronic bells.
When it comes to the buzzer’s operation, it converts electrical energy into sound energy.
When a voltage is applied to the buzzer, the piezo crystal inside the plastic casing expands and contracts. This causes the plate near the crystal to vibrate, and the sound you hear is the result of that vibration.
Changing the frequency of the buzzer alters the speed of the vibration, resulting in a variety of sounds.
So it came down to the buzzer. Buzzers are classified into two types. There are two types of buzzers: active and passive.
We will now understand the distinction between these two types in the following section of this blog.
Difference Between Active And Passive Buzzer
As I mentioned earlier, there are many types of buzzers. Active buzzer and passive buzzer.
Talking about the active buzzer, it has an inbuilt oscillating source. Active buzzer starts ringing as soon as you turn it on but in the case of the passive buzzer, they do not have an inbuilt oscillating source.
If you want a passive buzzer to produce a sound signal, you must give a different frequency to the buzzer.
Interfacing Buzzer With The Raspberry Pi
In this kit, we are getting an active buzzer with two pins. The first is the signal pin, and the second is the GND pin.
We connect the GND pin to the raspberry pi’s GND pins and the signal pin to the raspberry pi’s GPIO pin.
Please see the image below to understand the interfacing diagram.
Python Code For Raspberry Pi
In this section, we will be talking about the python code that we can run on the raspberry pi to turn on the buzzer.
import RPi.GPIO as GPIO import time Buzzer = 17 # pin17 def initial_setup (pin): global BuzzerPin BuzzerPin = pin GPIO.setmode(GPIO.BCM) # Numbers GPIOs by physical location GPIO.setup(BuzzerPin, GPIO.OUT) GPIO.output(BuzzerPin, GPIO.HIGH) def on(): GPIO.output(BuzzerPin, GPIO.LOW) def off(): GPIO.output(BuzzerPin, GPIO.HIGH) def beep(duration): on() time.sleep(duration) off() time.sleep(duration) initial_setup(Buzzer) for x in range(10): beep(x)
To work with this module, we’re going to use the code above. We’ve written a few functions to interact with the buzzer module in this code. I’ve explained those functions in the section below.
This function is used to define the pin’s mode and state.
We defined a global variable in this function. The reason for defining the global variable here is that we want to use the data from this function outside of this function as well, which we cannot do if the variable is not global.
That is why we have made that variable global. As a result, we can make use of that variable in other functions as well.
The data from the function’s input parameter is then passed to this Global Variable.
That input parameter is merely an integer value. And
As previously stated, the buzzer is the output device, which is why we pass the GPIO.OUT parameter, along with the pin number, to this GPIO.setup function.
ON And Off Function
As the name implies, we are using this function to turn on or turn off the buzzer module.
In this function, we call the On and Off functions. This function also receives a flot parameter. The delay time between the two functions will be determined by this parameter (on and off).
In other words, that function will activate the buzzer module for the duration of the time period specified.
So these are the functions we’re using to activate the buzzer module. If you have any questions, please leave them in the comments section.
Conclusion: Buzzer With The Raspberry Pi
As a result, we learned how to connect the buzzer to the raspberry pi in this manner. If you have any questions, please leave them in the comments section.
How To Use The IR Sensor With The Raspberry Pi?
We’ve all seen infrared sensors, which can be found in TV remote controls. These IR are used to send data to IR receivers, but do you understand how they work?
Normally, all objects emit some form of thermal radiation in the infrared spectrum.
This type of radiation is invisible to our eyes, but it can be detected by infrared sensors.
This sensor has two modules. Those modules are as follows:
1. IR receiver
2. IR transmitter
IR Transmitter or IR LED
An infrared transmitter (IR LED) is a light emitting diode (LED) that emits infrared radiation.
Even though an IR LED appears to be a regular LED, the radiation it emits is invisible to the naked eye.
IR Receiver or Photo-diode
Infrared receivers, also known as infrared sensors, detect the radiation emitted by infrared transmitters. Infrared receivers are classified into two types: photodiodes and phototransistors.
The gain amplifier is the third component of the IR sensor.
When the signal quality is very low, the gain amplifier becomes very important. It boosts the week signal and produces high-quality signals. This output of the gain amplifier is connected to the potentiometer.
If your module’s output is not clear, You can rotate this potentiometer to boost the weak signals.
So that was the introduction to the IR receiver; in the next part of the video, we’ll talk about how to connect the IR sensor to the Raspberry Pi board.
Interfacing The IR Sensor With The Raspberry Pi
The IR sensor included in this kit has three output pins. Two of those three pins are power pins, and one is an output pin.
We’ll have to use the level converter here. This is due to the Raspberry Pi’s inability to handle more than 3.3v on its GPIO pin.
As a result, we used the Level Converter to protect the raspberry pi from overvoltage and damage.
Please see the image below to understand the buzzer-to-Raspberry Pi interfacing diagram.
So, this was about the interfacing of the Sensor with the Raspberry pi.
Python Code For IR Sensor
import RPi.GPIO as GPIO import time sensor = 18 GPIO.setmode(GPIO.BCM) GPIO.setup(sensor,GPIO.IN) print ("IR Sensor Ready.....") print (" ") try: while True: if GPIO.input(sensor): print ("Object Detected") time.sleep(0.2) else: print ("There is no object.") except KeyboardInterrupt: GPIO.cleanup()
Conclusion- How To Use The IR Sensor With The Raspberry Pi?
So, in this way, we learned the fundamentals of the IR sensor module. If you have any questions, please leave them in the comments section.
How To Interface The PIR Sensor With The Raspberry Pi?
You may have heard about the burglar system. Those burglar systems are designed to detect the motion. When a moving object comes in front of these systems, it detects the presence of the object and pass high-level signals to the controller.
But how does the PIR sensor detects the motion of the human or animal? PIR sensor is designed to measure the infrared signals. It has two slots made of pyroelectric material. The more detailed information about the pyroelectric material has been shared in the booklet of this. You can refer to the booklet to know more about this material.
The voltage applied to this pyroelectric material changes as per the change in the temperature.
When the sensor is powered and there is no any moving object then the sensor detects the three will not be change in the output of the two slots of the PIR sensor.
when there will be change in the temperature in the sensing area of the sensor then the output of first slot of the PIR sensor changes and that change make the first slot to generate high electrical signals. Which results into positive differential change in the output of those two slots.
when the object that caused the change in the sensing area, leaves the sensing area then the reverse happens. Whereby the sensor generates the negative differential change.
This change in the output then will be given to the controller where the controller executes the required operations based on the input received.
So this was about the working principle of the PIR sensor, In the next part of the video we will talk about the interfacing of the PIR sensor with the raspberry Pi.
Interfacing The PIR Sensor With The Raspberry Pi
There are three output pins on the PIR sensor. Unlike the level converter we used in the IR sensor interfacing with the raspberry pi, we will need to use the level converter in the PIR sensor interfacing with the raspberry pi.
The image below should dispel any doubts you have about connecting the PIR sensor to the Raspberry Pi.
Aside from that, if you have any questions, please leave them in the comments section.
Raspberry Pi Python Code For Raspberry Pi
from gpiozero import MotionSensor pir = MotionSensor(23) while True: pir.wait_for_motion() print("You moved") pir.wait_for_no_motion()
To communicate with the PIR sensor, we use the code above. In this code, we used a GPIOZERO module and the MotionSensor class of the GPIOZERO module.
In the preceding line of code, we are telling the module that we have connected the PIR sensor to Raspberry PI pin number 4.
Following that, we used a while loop, which ran a few lines of code continuously.
In this loop, we tell the library to read the data on the pin number to which the PIR sensor is connected and to print the sensor’s output on the serial monitor.
Talking about working principle of the IR sensor, when an object passes in front of it, the PIR sensor generates LOW-level signals.
We can use these signals to trigger any real-time event, but we’re not doing so here because we’re just learning the basics of the PIR Sensor. As a result, we used the print method to print the output of the sensor.
Conclusion: How To Interface The PIR Sensor With The Raspberry Pi?
So, in this section of the blog, we learned how to connect the PIR sensor to the Raspberry Pi.
If you have any questions, please leave them in the comments section.
In the following section of the blog, we will learn how to connect the LCD to the Raspberry Pi.
How To Use The LDR Sensor With The Raspberry Pi?
The light-dependent resistor is nothing more than a resistor. And the value of this resistor varies with the intensity of the light.
You’ve probably seen the stud lights on the streets. LDR is used to detect light in those lights.
Application Of The LDR Sensor
As I mentioned earlier, LDR is used for light detection applications. Based on this feature, we can use it in a wide variety of projects. I have mentioned some of those applications below. Please take a look.
In a solar cell charging unit, the LDR can be used to detect the intensity of light. Depending on the readings the control unit receives from the LDR, the controlling unit can rotate the solar panel to generate maximum energy.
- Light Tracking Robot
This is a very popular example in which we can see the use of LDR. Those types of robots can be designed with the help of the LDR sensor.
You must have seen the stud lights on the streets. Those lights are used for indication purposes. LDRs are used to detect daylight in those lights.
Interfacing The LDR With The Raspberry Pi
The LDR is has two pins. To connect the module to the Raspberry Pi, connect one pin to the GPIO pin of the Raspberry Pi and the other to the GND pin of the Raspberry Pi.
Please refer to the image below to fully comprehend the connection diagram.
Raspberry Pi Python Code
The following code will be used to read the output of the LDR module.
In this code, we used the same method to define the pin number that we used in the previous section.
Please use the code below and let me know if you have any problems.
import RPi.GPIO as GPIO import time sensor = 18 GPIO.setmode(GPIO.BCM) GPIO.setup(sensor,GPIO.IN) print ("LDR Sensor is Ready.....") print (" ") try: while True: if GPIO.input(sensor): print ("Light Detected") time.sleep(0.2) else: print ("There is no Light.") except KeyboardInterrupt: GPIO.cleanup()
Conclusion: How To Interface The LDR Module With The Raspberry Pi?
As a result, we learned how to work with the LDR sensor’s interfacing with the Raspberry Pi. If you have any questions, please leave them in the comments section.
IR Flame Sensor
There are various types of flame sensors on the market. With this kit, you will receive an infrared flame sensor.
In a fire system, an infrared flame sensor is used. They detect fires and send a signal to the controlling unit, which then takes the necessary action to prevent fire damage. But, did you know… How does the infrared flame sensor detect fire?
The answer is that infrared sensors are designed to detect visible gases in the infrared spectral band.
When an explosion occurs, the light emitted by the flames is reflected in the infrared spectral band, which generates a flame sensor signal based on the light’s received frequency.
It was about the introduction of the flame sensor; we will also learn about interfacing the flame sensor with the raspberry pi in the next section of the blog.
Interfacing The Flame Sensor With The Raspberry Pi
As previously stated, flame sensors are used to detect fires. When they detect fire, they send out high-level signals. In this section, we’ll look at how to connect the flame sensor to the Raspberry Pi.
The flame sensor will generate either high- or low-level signals, allowing us to connect it to any GPIO pin on the Raspberry Pi.
The image below shows how to connect the Flame sensor to the Raspberry Pi.
Python Code For The Flame Sensor – Raspberry Pi Interfacing
Because the flame sensor will generate either a High- or Low-Level Signal, we can use the following simple code to interact with it.
import RPi.GPIO as GPIO import time #GPIO SETUP channel = 21 GPIO.setmode(GPIO.BCM) GPIO.setup(channel, GPIO.IN) def callback(channel): print("flame detected") GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300) # let us know when the pin goes HIGH or LOW GPIO.add_event_callback(channel, callback) # assign function to GPIO PIN, Run function on change # infinite loop while True: time.sleep(1)
We have used three main functions in the above code. Those are as follows.
GPIO.add_event_detect and GPIO.add_event_callback
The library function GPIO.add event detect is used to detect a specific event. When that event occurs, the GPIO.add event callback function handles it.
This function is a user-defined function that prints the sensor output when a specific movement is detected.
Conclusion – Flame Sensor Interfacing With The Raspberry Pi
As a result, we learned how to connect the flame sensor to the Raspberry Pi in this manner. Please let us know if you have any questions.
In the following section of this blog, we will learn how to connect a servo motor to a Raspberry Pi.
Servo Motor SG90 With The Raspberry Pi
The SG-90 microservo is a small, lightweight rotary electrical device with a high output power. Its shaft can rotate approximately 180 degrees (90 degrees in each direction) by sending a coded signal to the servo. The servo motor is a common piece of hardware found in model boats, radio-controlled aircraft, industrial applications, and robots.
If you want to know the working principle of the servo motor then please check the booklet.
We mentioned everything about the servos in details in the booklet. In the next part of the blog we will learn to interface the servo motor with the raspberry Pi.
How To Interface The Servo Motor With The Raspberry PI
Connecting an SG90 micro servo to the RPi is simple. We will only require an external power supply because the RPi GPIO does not provide enough current.
Please see the image below to understand how to connect the servo motor to the Raspberry Pi.
How To Program The Servo Motor?
The Following Python code can be used to program the servo motor. You can write your own PWM code or use the following Code.
import RPi.GPIO as GPIO import time P_SERVO = 22 # adapt to your wiring fPWM = 50 # Hz (not higher with software PWM) a = 10 b = 2 def setup(): global pwm GPIO.setmode(GPIO.BCM) GPIO.setup(P_SERVO, GPIO.OUT) pwm = GPIO.PWM(P_SERVO, fPWM) pwm.start(0) def setDirection(direction): duty = a / 180 * direction + b pwm.ChangeDutyCycle(duty) print "direction =", direction, "-> duty =", duty time.sleep(1) # allow to settle print "starting" setup() for direction in range(0, 181, 10): setDirection(direction) direction = 0 setDirection(0) GPIO.cleanup() print "done"
In the next part of the blog we will learn how to interface the heart sensor with the raspberry Pi.
Heart Sensor With The Raspberry Pi
The heart rate sensor detects the living object’s heart rate. It operates in a very straightforward manner. This sensor has two sides: one side has an LED and an ambient light sensor, and the other side has some circuitry.
This circuitry is in charge of amplification and noise cancellation.
The LED on the sensor’s front side is placed over a vein in our human body.
This can be your fingertip or your eartip, but the sensor must be placed directly on top of a vein.
Now the LED emits light, which falls directly on the vein. Only when the heart is pumping the blood flow through the veins, so if we monitor the flow of blood, we can also monitor the heart beats.
If blood flow is detected, the ambient light sensor will pick up lighter colors because they will be reflected by the blood; this minor change in received light is analyzed over time to determine our heart beats.
So, this is how the heart rate sensor functions. In the following section of the blog, we will learn how to connect the heart rate sensor to the Raspberry Pi.
How To Interface The Heart Rate Sensor With The Raspberry Pi?
As you can see in the following image, the heart rate sensor has three pins. Out of these three pins one is signal pin and other pins are power pins. Please see the following image to understand the interfacing diagram properly.
How To Program The Raspberry Pi?
In this code, we’ve included two codes. The I2C display code is one, and the ADC module code is another.
You can connect the I2C LCD Display and the Heart rate sensor to the raspberry pi and can start using the below code.
Note: If you are using the I2C Display then don’t forgot turn on the I2C Communication on your raspberry Pi otherwise the following code will not work.
ADDRESS = 0x27 import smbus from time import sleep class i2c_device: def __init__(self, addr, port=I2CBUS): self.addr = addr self.bus = smbus.SMBus(port) # Write a single command def write_cmd(self, cmd): self.bus.write_byte(self.addr, cmd) sleep(0.0001) # Write a command and argument def write_cmd_arg(self, cmd, data): self.bus.write_byte_data(self.addr, cmd, data) sleep(0.0001) # Write a block of data def write_block_data(self, cmd, data): self.bus.write_block_data(self.addr, cmd, data) sleep(0.0001) # Read a single byte def read(self): return self.bus.read_byte(self.addr) # Read def read_data(self, cmd): return self.bus.read_byte_data(self.addr, cmd) # Read a block of data def read_block_data(self, cmd): return self.bus.read_block_data(self.addr, cmd) # commands LCD_CLEARDISPLAY = 0x01 LCD_RETURNHOME = 0x02 LCD_ENTRYMODESET = 0x04 LCD_DISPLAYCONTROL = 0x08 LCD_CURSORSHIFT = 0x10 LCD_FUNCTIONSET = 0x20 LCD_SETCGRAMADDR = 0x40 LCD_SETDDRAMADDR = 0x80 # flags for display entry mode LCD_ENTRYRIGHT = 0x00 LCD_ENTRYLEFT = 0x02 LCD_ENTRYSHIFTINCREMENT = 0x01 LCD_ENTRYSHIFTDECREMENT = 0x00 # flags for display on/off control LCD_DISPLAYON = 0x04 LCD_DISPLAYOFF = 0x00 LCD_CURSORON = 0x02 LCD_CURSOROFF = 0x00 LCD_BLINKON = 0x01 LCD_BLINKOFF = 0x00 # flags for display/cursor shift LCD_DISPLAYMOVE = 0x08 LCD_CURSORMOVE = 0x00 LCD_MOVERIGHT = 0x04 LCD_MOVELEFT = 0x00 # flags for function set LCD_8BITMODE = 0x10 LCD_4BITMODE = 0x00 LCD_2LINE = 0x08 LCD_1LINE = 0x00 LCD_5x10DOTS = 0x04 LCD_5x8DOTS = 0x00 # flags for backlight control LCD_BACKLIGHT = 0x08 LCD_NOBACKLIGHT = 0x00 En = 0b00000100 # Enable bit Rw = 0b00000010 # Read/Write bit Rs = 0b00000001 # Register select bit class lcd: #initializes objects and lcd def __init__(self): self.lcd_device = i2c_device(ADDRESS) self.lcd_write(0x03) self.lcd_write(0x03) self.lcd_write(0x03) self.lcd_write(0x02) self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE) self.lcd_write(LCD_DISPLAYCONTROL | LCD_DISPLAYON) self.lcd_write(LCD_CLEARDISPLAY) self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT) sleep(0.2) # clocks EN to latch command def lcd_strobe(self, data): self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT) sleep(.0005) self.lcd_device.write_cmd(((data & ~En) | LCD_BACKLIGHT)) sleep(.0001) def lcd_write_four_bits(self, data): self.lcd_device.write_cmd(data | LCD_BACKLIGHT) self.lcd_strobe(data) # write a command to lcd def lcd_write(self, cmd, mode=0): self.lcd_write_four_bits(mode | (cmd & 0xF0)) self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0)) # write a character to lcd (or character rom) 0x09: backlight | RS=DR< # works! def lcd_write_char(self, charvalue, mode=1): self.lcd_write_four_bits(mode | (charvalue & 0xF0)) self.lcd_write_four_bits(mode | ((charvalue << 4) & 0xF0)) # put string function with optional char positioning def lcd_display_string(self, string, line=1, pos=0): if line == 1: pos_new = pos elif line == 2: pos_new = 0x40 + pos elif line == 3: pos_new = 0x14 + pos elif line == 4: pos_new = 0x54 + pos self.lcd_write(0x80 + pos_new) for char in string: self.lcd_write(ord(char), Rs) # clear lcd and set to home def lcd_clear(self): self.lcd_write(LCD_CLEARDISPLAY) self.lcd_write(LCD_RETURNHOME) # define backlight on/off (lcd.backlight(1); off= lcd.backlight(0) def backlight(self, state): # for state, 1 = on, 0 = off if state == 1: self.lcd_device.write_cmd(LCD_BACKLIGHT) elif state == 0: self.lcd_device.write_cmd(LCD_NOBACKLIGHT) # add custom characters (0 - 7) def lcd_load_custom_chars(self, fontdata): self.lcd_write(0x40); for char in fontdata: for line in char: self.lcd_write_char(line)
Conclusion : Orange Raspberry Pi Beginner Kit
In this way, we have discussed all of the components included with this Raspberry Pi beginner kit.
Please contact us if you have any questions..