nextguy
Posts: 20
Joined: Sun Aug 11, 2019 10:38 am

Need help with a project - synchronized targets with hit detection

Sun Aug 11, 2019 10:39 am

Hi all,

My apologies if this project has already been discussed but I've been looking for a solution for a few days and have found none. I am very new to RPi and currently own one that I use exclusively for Pi-hole. i have no experience working with sensors.

The project that I want to build seems easy enough. I want to have 4 physical targets with a light on each. The RPi will randomly select a number from 1 to 4 and then light up the corresponding target. When the target is hit by a ball, the RPi will turn off the light on the target, generate another number from 1 to 4 and light that target. This continues indefinitely.

I looked into embedding an SW420 into each target but can't figure out how to connect each SW420 into the RPi or how I can determine which SW420 was triggered.

If you have any advice it would be much appreciated.

Thanks!

pcmanbob
Posts: 7071
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Need help with a project - synchronized targets with hit detection

Sun Aug 11, 2019 11:44 am

Hi.

So I am assuming you are talking about a SW420 vibration sensor ( always a good idea to post a link to the device you intend to use ),

So assuming the sensors you have can work at 3.3v you simply need to connect power and ground to the sensor and then connect the data out pin to a gpio input with each sw420 using a different gpio input , now you have 4 inputs that you can check to see which is activated in you program.

This is a very simple test program which should print which sensor is triggered, it is untesed as I don't have any sw420 sensors to test it with.

Code: Select all

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(12, GPIO.IN) #sensor1
GPIO.setup(16, GPIO.IN) #sensor2
GPIO.setup(20, GPIO.IN) #sensor3
GPIO.setup(21, GPIO.IN) #sensor4

while True:

    if GPIO.input(12) == 1:
        print ("sensor 1 triggered")
        
    if GPIO.input(16) == 1:
        print ("sensor 2 triggered")   

    if GPIO.input(20) == 1:
        print ("sensor 3 triggered")
        
    if GPIO.input(21) == 1:
        print ("sensor 4 triggered") 

    time.sleep(0.1)
    
NOTE. if your sensors will only work at 5v please let us know as you will need some extra circuitry to connect the sensors to the pi to prevent damage to the pi gpio.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

nextguy
Posts: 20
Joined: Sun Aug 11, 2019 10:38 am

Re: Need help with a project - synchronized targets with hit detection

Sun Aug 11, 2019 12:05 pm

Brilliant thank you so much for your help. Although I understood maybe only 70% of what you wrote, it is a very solid start for my project. I was doing more research since my original post and thought that maybe I would have to put an Arduino board between the sensors and the Pi. So it sounds like if I can find 3.3v sensors then this is an unnecessary step.

Another question, when connecting the sensor pins to the gpio inputs, should I just use a jumper wire? The distance between the sensors and the Pi will probably be a good 10 feet to avoid accidental damage.

Also not sure if you or someone else can answer this, but knowing my use case, is the sw420 vibration sensor a suitable sensor? As mentioned, I will have targets that need to detect an impact. I think I would need to play with thresholds to only trigger on the initial contact and ignore aftershocks. Is there a more appropriate piezoelectric sensor that could do this?

Thank you again!

pcmanbob
Posts: 7071
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Need help with a project - synchronized targets with hit detection

Sun Aug 11, 2019 12:41 pm

nextguy wrote:
Sun Aug 11, 2019 12:05 pm
Brilliant thank you so much for your help. Although I understood maybe only 70% of what you wrote, it is a very solid start for my project. I was doing more research since my original post and thought that maybe I would have to put an Arduino board between the sensors and the Pi. So it sounds like if I can find 3.3v sensors then this is an unnecessary step.

Even if you use 5v sensors you can still connect them to the pi gpio you just need to use a potential divider circuit to reduce the 5v to 3v
Another question, when connecting the sensor pins to the gpio inputs, should I just use a jumper wire? The distance between the sensors and the Pi will probably be a good 10 feet to avoid accidental damage.

You could use LAN patch cable its twisted pairs will help reduce noise on the cable and its readily available, while testing ordinary Dupont cables will do while its on your desk .
Also not sure if you or someone else can answer this, but knowing my use case, is the sw420 vibration sensor a suitable sensor? As mentioned, I will have targets that need to detect an impact. I think I would need to play with thresholds to only trigger on the initial contact and ignore aftershocks. Is there a more appropriate piezoelectric sensor that could do this?

I don't see why the sw420 should not work , you could just buy 1 and do some testing for yourself , it would also give you the chance to learn along the way, you could write your program in such a way as to ignore after shocks.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
neilgl
Posts: 1106
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Need help with a project - synchronized targets with hit detection

Sun Aug 11, 2019 7:19 pm

Could you just use some cheap micro switches behind hinged panels for the targets?

nextguy
Posts: 20
Joined: Sun Aug 11, 2019 10:38 am

Re: Need help with a project - synchronized targets with hit detection

Sun Aug 11, 2019 10:25 pm

So this is the SW420 that I was looking at:

https://www.elecrow.com/vibration-senso ... p-525.html

The specs say that it needs an input voltage of 3.3-5V which I guess means I can use these with an RPi directly without extra circuitry?

Also, I'm confused about which GPIO pins I should be connecting the sensor to. Can I put 4 of them on an RPi? Like on this diagram, which pins should I be connecting the 4 sensors?

https://cdn.sparkfun.com/r/600-600/asse ... pinout.jpg

I was looking at using switches but the targets aren't supposed to move at all after being struck so they aren't hinged. I thought a vibration sensor made the most sense for my use case.

pcmanbob
Posts: 7071
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Need help with a project - synchronized targets with hit detection

Mon Aug 12, 2019 12:00 am

You just need to connect 3.3v and ground to each sensor then connect the D0 pin of each sensor to the relevant gpio pin, as listed in the code.

like this

Image
Last edited by pcmanbob on Wed Aug 14, 2019 8:10 am, edited 1 time in total.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

nextguy
Posts: 20
Joined: Sun Aug 11, 2019 10:38 am

Re: Need help with a project - synchronized targets with hit detection

Mon Aug 12, 2019 10:16 am

Ok I get it now!

Sorry one more question. What is the easiest/cheapest way to connect the sensors to the 3.3v and GND pin? There are going to be 4 sensors so do I need to use like a breadboard or something?

Thank you so much!

pcmanbob
Posts: 7071
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Need help with a project - synchronized targets with hit detection

Mon Aug 12, 2019 12:18 pm

For testing yes you could use a breadboard and some dupoint cables, but in your final build you really need to use a soldered solution for reliability, you could just use some strip-board and solder all the wires to that and then use a 40 way IDC connector to connect that to the pi gpio.


examples but not recommendations for seller.
https://shop.pimoroni.com/products/gpio ... -b-40-pins
https://www.kitronik.co.uk/2439-strip-b ... 250mm.html
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Bope
Posts: 52
Joined: Sat Jul 06, 2019 2:57 am

Re: Need help with a project - synchronized targets with hit detection

Mon Aug 12, 2019 7:21 pm

I am new to this also but for my first project I was planning on using a terminal block for all my GPIO connections.

For your after shocks look up switch bounce. This will help you understand how to code for this. essentially you need some time.sleep statements.

https://www.amazon.com/gp/product/B01M2 ... KMW5&psc=1

nextguy
Posts: 20
Joined: Sun Aug 11, 2019 10:38 am

Re: Need help with a project - synchronized targets with hit detection

Mon Aug 12, 2019 9:34 pm

Oh that terminal block looks cool. I didn't know it existed.

Thanks!

User avatar
neilgl
Posts: 1106
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Need help with a project - synchronized targets with hit detection

Mon Aug 12, 2019 9:58 pm

I would go with wago connectors - a lot cheaper

boyoh
Posts: 1341
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: Need help with a project - synchronized targets with hit detection

Mon Aug 12, 2019 10:50 pm

Give your ball game some interest, Have the
Light flashing, controlled by a variable controlled
Pulse generator clock. You will only score if the
Ball hits the target when the light is on.
Controlling the light on time will give you some
Interest to the game.

Regards BoyOh
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

nextguy
Posts: 20
Joined: Sun Aug 11, 2019 10:38 am

Re: Need help with a project - synchronized targets with hit detection

Tue Aug 13, 2019 2:46 am

boyoh wrote:
Mon Aug 12, 2019 10:50 pm
Give your ball game some interest...
My application is more for athletic training than for amusement. I want the targets to light up only after the previous target has been hit. And only the lit target is scored. I imagine having a display that shows the time it takes between target lighting to target hit, generate stats such as average time, and my ultimate would be to have a speed gun. But that is pie in the sky thinking for now.

boyoh
Posts: 1341
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: Need help with a project - synchronized targets with hit detection

Tue Aug 13, 2019 10:22 pm

I think you are waiting for someone to design the complete circuit for you, this won’t happen, the
Forum is here to point you in the right direction.
And not do the research for you. So learn some
Basic electronics then you will understand what
You are being told.

Regards BoyOh
Retired Electrical / Electronic Technician
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

nextguy
Posts: 20
Joined: Sun Aug 11, 2019 10:38 am

Re: Need help with a project - synchronized targets with hit detection

Wed Aug 14, 2019 12:34 am

boyoh wrote:
Tue Aug 13, 2019 10:22 pm
I think you are waiting for someone to design the complete circuit for you...
No that's not what I want at all. I was just asking if I was choosing the right parts and to help understand how the wiring would work. My last post about the nature and function of my project was only in response to your previous post. I am good now and I am not requesting anymore advice unless someone wants to offer it.

Thanks to all that helped with your valuable insight!

Return to “Beginners”