Sensing Conductive Metal Surfaces with SPIKE Prime

Anton

Updated on:

spike detect conductive surface

Imagine your SPIKE robot has the superpower to detect conductive surfaces, like it’s feeling its way through a maze with metal tape and tin foil objects. This isn’t a scene from a sci-fi movie; it’s entirely possible with the right tools and a bit of programming. Today, we’re diving into how you can give your SPIKE Prime robot this incredible ability, using two wires and an ingenious little board known as the LMS-ESP32.

The Magic Behind Detecting Conductive Surfaces

At the heart of this exploration is a basic principle: conductive surfaces, like metal tape and tin foil, allow electricity to flow through them. By exploiting this property, we can make our robots “feel” these surfaces. How? By pushing two wires against the surface. One wire acts as our ground (0V), and the other serves as our detective, sensing the presence of 0V when it touches something conductive.

These wires connect to the Input Output (IO) pins on an LMS-ESP32 board. This board is where the magic happens, transforming your SPIKE Prime into a conductivity-sensing wizard.

The Role of the LMS-ESP32 in Sensing conductivity

The LMS-ESP32 has an embedded microprocessor with customizable IO pins. You can configure one of these IO pins to sense ground (zero volt). Then touch the conductive surface with a ground pin and a sensing. The conductive surface creates a circuit that changes the input pin’s state. It’s like flipping a switch.

What you do is define the sensing pin as an input pin. Then make it default ‘high’ with a pull-up resistor. The LMS-ESP32 will read it as 1 if nothing is connected, and the value will read 0 if you connect the pin to GND (0V). You don’t need to solder, only configure the pin like this.

from machine import Pin

# Create an input pin on pin #2, with a pull-up resistor.
p2 = Pin(2, Pin.IN, Pin.PULL_UP)

# Read and print the pin value. 
# Returns 1 if the wire is not connected to GND.
print( p2.value() )

Sending the pin value to Pybricks on LEGO SPIKE Prime

But how do we communicate this discovery back to our SPIKE Prime? Enter the pupremote protocol – a bridge that sends pin values directly to Pybricks, the programming language for LEGO robotics. With this setup, you’re not just limited to sensing conductivity; you’re opening up a world of possibilities for switches.

The easiest way to start using PUPremote on LMS-ESP32 is by installing Thonny. Using Thonny, upload two files to your board: pupremote.py and lpf2.py. Next, create a main.py file containing your pin configuration:

from pupremote import  PUPRemoteSensor, SPIKE_ULTRASONIC
from machine import Pin

# configure our pin
p2 = Pin(2, Pin.IN, Pin.PULL_UP)
    
# create a function that returns pin values as individual values (tuple)
def read_pins(*argv):
    return p2.value()

# set up an emulated LEGO sensor
p=PUPRemoteSensor(sensor_id=SPIKE_ULTRASONIC)

# ensure the output of our function is passed as sensor data
p.add_command('read_pins', to_hub_fmt="b")

### Main loop: keep processing and sending sensor data
while(True):
    connected=p.process()

Coding the conductive surface sensor on SPIKE with Pybricks

With just a few lines of Python code, you can bring this concept to life. Programming in Pybricks is straightforward and accessible. It is perfect for Robocup coaches and STEAM teachers looking to inspire their students with hands-on learning experiences.

from pybricks.hubs import PrimeHub
from pybricks.parameters import Port
from pybricks.iodevices import PUPDevice

hub = PrimeHub()

conductivity_sensor = PUPDevice(Port.A)

def read_pin():
    return 0 if conductivity_sensor.read(0) else 100

while 1:
    hub.display.pixel( 
            0, 0,  # Light up top left pixel
             read_pin() # ...if pin 2 is connected to GND
            )

Once you have the Python file running, you can even read the pin in Pybricks word blocks!

Why You Need an LMS-ESP32 Board

The LMS-ESP32 board is more than just a piece of hardware; it’s an investment in your robotics toolkit that unlocks new potentials for learning and electronics. Whether you’re coaching a Robocup team or teaching STEAM subjects, integrating this technology into your curriculum can elevate your students’ learning experience.

By choosing the LMS-ESP32 board for your projects, you’re opening up a world of possibilities with custom electronics. It’s about creating learning experiences that inspire curiosity, creativity, and critical thinking. That’s why we teamed with Pybricks and giving away a 10% coupon code for a Pybricks blocks lifetime license!

Bonus program: detect multiple conductive surfaces

Here’s a bonus program that sends all IO pin values from a list across to SPIKE Prime

from pupremote import  PUPRemoteSensor, SPIKE_ULTRASONIC
from machine import Pin

pins = []
for n in (2, 26,27,32,33): # List all your desired pins here!
    pins += [Pin(n, Pin.IN, Pin.PULL_UP)]
    
def read_pins(*argv):
    return tuple([pin.value() for pin in pins])

p=PUPRemoteSensor(sensor_id=SPIKE_ULTRASONIC)
format = f"{len(pin_nums)}b"
p.add_command('read_pins', to_hub_fmt=format)

while(True):
    connected=p.process()

What will you build?

Detecting conductive surfaces with SPIKE Prime opens up new avenues for exploration in robotics education. It demonstrates how combining simple components like wires with sophisticated technology like the LMS-ESP32 board can create powerful learning tools.

We’re eagerly awaiting your builds. Please tag us or send a picture!

Like this article? Help us make more!

Your support matters

We appreciate your support on Patreon and YouTube! Small things count.

Become a Patron

Don't miss a thing

Subscribe below to get an email when we publish a new article.

Share this with someone who needs to know

2 thoughts on “Sensing Conductive Metal Surfaces with SPIKE Prime”

  1. Really wonderful project!
    I have a question that as pybricks block get esp-lms board’s signal, openmv can communicate with pybricks block code?

    Reply

Leave a Reply

Item added to cart.
0 items - 0.00