Example Code for Raspberry Pi – Conductivity Sensor Switch

Last revision 2026/01/20

The article provides an in-depth guide on using a conductivity sensor switch with Raspberry Pi, detailing hardware setup, Python coding, and creative applications such as fruit pianos and sensor lights.

In this section we will introduce the conductivity switch sensor. We can use it to test the conductivity of the object, and can also use it as a switching device for special occasions.

Introduction

The conductivity switch is to conduct through two exposed conductors. When the two sides are connected through a certain medium, they are turned on. And the sensor can detect whether there is an object connection between the two poles. When the resistance value of the connected object is less than 10M, it can output a high level. You can use it to make a fruit piano, music wind chime, handshake sensor light and other interesting applications.

Hardware Preparation

Wiring Diagram

  • Connect the Raspberry Pi correctly to devices such as the screen, power, keyboard and mouse.

  • Install the Raspberry Pi IO expansion board on the Raspberry Pi and connect the LED light module to the No. 12 digital port on the board, and the conductivity switch sensor module to the No. 8 digital port. When we touch to connect the two alligator clips with our hands or other conductive objects, we can see the corresponding phenomenon.

  • Expansion Board Connection

Operating Principle

Change the level signal as a switch by conducting two metal sheets.

Schematic

Sample Code

  • Open Thonny Python IDE to copy the following program into it


    import RPi.GPIO as GPIO    # Import the python module provided by the Raspberry Pi
    import time    # Import time package to control flicker
    
    LED=12       # Define the pin number to which the LED is connected
    Electrical_switch = 8                        # Define the pin number to which the switch is connected
    
    GPIO.setmode(GPIO.BCM)                    # Set GPIO mode, BCM mode is common to all Raspberry Pi
    GPIO.setup(Electrical_switch,GPIO.IN)      # Set GPIO12 to output mode
    GPIO.setup(LED,GPIO.OUT)                    # Set GPIO8 to input mode
    GPIO.output(LED,GPIO.HIGH)                  #Define LED original value
    
    while True:                               # Execute the following commands in an infinite loop
        key = GPIO.input(Electrical_switch)
        if (key ):                            # Judge whether the button is pressed
            GPIO.output(LED,GPIO.LOW)         # Button pressed, start the micro vibrator
        else :                     # If GPIO8 is low (that is, the button is released), execute the following statement
            GPIO.output(LED,GPIO.HIGH)        # Not start the micro vibrator
    time.sleep(0.01)                   # Delay one second, here is to control the frequency of the query key
    ```


-	Click ‘Save’ after copying

![](https://dfimg.dfrobot.com/nobody/wiki/a77f9ba46968c9e96948f608784b1d57.PNG)


-	Set the file name and the save path you like

![](https://dfimg.dfrobot.com/nobody/wiki/ea0adcbbf49e09264e9dd27276001e89.JPG)

-	Press ‘Run’ or ‘Stop’ the program

![](https://dfimg.dfrobot.com/nobody/wiki/ac33363aa24ae99c13a1b5e58e08ed3d.PNG)


-	After running it, check the corresponding phenomenon


http://https//www.bilibili.com/video/BV1z54y1b763/

Was this article helpful?

TOP