본문 바로가기
라즈베리파이

[RPI] How to use Inductive Proximity Sensor with Raspberry Pi 3 Model B

by YJHTPII 2024. 4. 29.
반응형
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)

 

https://medium.com/@TanakittiSachati/how-to-use-inductive-proximity-sensor-with-raspberry-pi-3-model-b-python-539ecc731bf3

 

How to use Inductive Proximity Sensor with Raspberry Pi 3 Model B [Python]

Easy steps to create your own Inductive Proximity Sensor with Raspberry Pi 3 Model B on Python

medium.com

 

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.

 

Hardware Preparetion

  1. Raspberry Pi model B with Raspbian
  2. Inductive Proximity Sensor (LJ12A3–4-Z/BX)
Example of Inductive Proximity Sensor (ebay)
 

How to set up Raspberry Pi

Pin of Raspberry pi 3 model B (Imgae source)

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.

The circuit of Raspberry pi and the sensor

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

  1. 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

 

반응형

댓글