import time
import RPi.GPIO as GPIO
# Pin of Input
GPIOpin = -1
# Initial the input pin
def initialInductive(pin):
global GPIOpin
GPIOpin = pin
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIOpin,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
print("Finished Initiation")
print(GPIOpin)
# Detect Metal
def detectMetal():
if(GPIOpin != -1):
state = GPIO.input(GPIOpin)
if state:
print("Metal Detected")
else :
print("Metal Not Detected")
else:
print("Please Initial Input Ports")
# test module
if __name__ == '__main__':
pin = 17
initialInductive(pin)
while True:
detectMetal()
time.sleep(0.2)
Introduction
This is my first article about Raspberry pi and I am a new programmer for raspberry as well but I took some courses and self-study to do my senior project so I have some knowledge about this topic and I would like to share and exchange some knowledge. If there are any problems or errors, please feel free to give me a comment or suggestion.
How to set up Raspberry Pi
The first thing that you need to know is the name of pins of the Raspberry pi 3 model B. Today, we work on GPIO (General-Purpose Input/Output) pins (Details) and the number of pins is used in the code as well.
I connect the orange line with the 4th pin (5V Power), black line with the 6th pin (Ground), and blue line with the 11th pin (GPIO 17).
How to set up python code
- Install the related library which is RPi.GPIO
sudo apt-get update
sudo apt-get install rpi.gpio
2. Create a python file named test.py
nano test.py
3. Put some code
import time
import RPi.GPIO as GPIO
# Pin of Input
GPIOpin = -1
I import the time library to delay when I print the output value and the RPI.GPIO is used to get the input value.
# Initial the input pin
def initialInductive(pin):
global GPIOpin
GPIOpin = pin
GPIO.setmode(GPIO.BCM)
GPIO.setup(GPIOpin,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
print("Finished Initiation")
print(GPIOpin)
I create the “initialInductive” function to initial the pin of raspberry pi easier.
# Detect Metal
def detectMetal():
if(GPIOpin != -1):
state = GPIO.input(GPIOpin)
if state:
print("Metal Detected")
else :
print("Metal Not Detected")
else:
print("Please Initial Input Pin")
I also wrote the “detectMetal” function that read the digital value from the input pin and then prints the value out. The state variable will have 2 value which is 1 or true so if it true, I will print “Metal Detected”. Otherwise, I will print “Metal Not Detected”
# test module
if __name__ == '__main__':
pin = 17
initialInductive(pin)
while True:
detectMetal()
time.sleep(0.2)
This is a code that I use to check all functions. I use the GPIO pin 17 (from the circuit) to test my code.
4. Have fun with the code
Note: Github of the project
'라즈베리파이' 카테고리의 다른 글
[RaspberryPi] sudo apt-get update error (0) | 2024.05.10 |
---|---|
Finding Raspberry Pi IP address using nmap (0) | 2023.08.20 |
[Raspberry Pi]Raspberry Pi 4에 Windows 11을 설치하는 방법 (0) | 2023.07.21 |
Raspberry pi에 docker 설치하기 (0) | 2023.07.13 |
Raspberry Pi 4에 Windows 11을 설치하는 방법 (0) | 2023.07.13 |
댓글