1
\$\begingroup\$

I am trying to make a digital dashboard for a motorcycle using a Raspberry Pi, a display and some sensors.

I am using this speed sensor.

When I put the sensor on the axis of the motor wheel and run the engine, it shows random values even if the wheel is fixed and not turning.

The magnetic field generated by the engine and the alternator is affecting GPIO value, so I am trying to move all that noise from the signal. I added a low pass filter.

The maximum of speed is 120km/h so the frequency should be at maximum fmax= S / ( R x 2 x Pi), S is the speed , R is the tire radius.

In my case fc = 13.71 Hz and (fc = 1/(2 x Pi x R x C))

The capacitor used is a 22uF 40V and the resistance R= 530 ohm.

When running the engine the GPIO pin is still detecting noise and is reading like 10 pulses per second. Would it be better if I add an LC low pass filter?

This is the electrical diagram of the sensor and the opotocoupler 4n25.

enter image description here

Can someone please tell me how to do the conditioning for this type of sensor?

The idea of adding a low pass filter comes from this video tutorial at min 5:41.

He made his own dashboard and it is on sale so the low pass filter should do the work and filter all that noise.

If nothing is wired (the Raspberry is supplied by the power supply so the Raspberry is totally isolated from the sensor and the engine's battery) and I run the engine and the GPIO is configured as pull up and an interrupt so it is conected to an internal pull up, it detects nothing but if I just add any type of wire (just simply to add an external pull up resistor without even connecting the sensor's output to the GPIO) the wire acts like an antennae and detects all the noise coming from the engine and the alternator. I tried also with the coaxial cable and got the same thing. The problem comes from the GPIO.

\$\endgroup\$
9
  • 3
    \$\begingroup\$ Hard to say for sure from the poor sensor datasheet, but it looks like an open-collector NPN output. Do you have a pull-up resistor on the output of the sensor? If so, what's the resistor value? Can you add a complete schematic of how you have everything hooked up? \$\endgroup\$
    – John D
    Jun 14, 2021 at 13:47
  • \$\begingroup\$ Usually there's a recommendation of what resistor value to pick, of the low ohm variety. If left floating it naturally won't behave well. You need to check the actual datasheet for that, not just some product brief. \$\endgroup\$
    – Lundin
    Jun 14, 2021 at 14:09
  • \$\begingroup\$ I didn't add any pull up resistor. I edited my post and added the complete schematic. and what if the sensor is universal and comes without a datadheet ? I used to work with this sensor ebay.com/itm/233085561415 but tried this new one because it is not inductive sensor so I thought that the noise will decrease \$\endgroup\$
    – mina
    Jun 14, 2021 at 14:13
  • \$\begingroup\$ The resistor should likely be a pull up to V+ of the Rasp Pi and not in series. Also what's the voltage levels of the output? Same as you put in? You might need a voltage divider. Or if it expects a certain current, a shunt resistor. Again, we need to check the actual datasheet. If the sensor doesn't have one, then it's good for nothing. \$\endgroup\$
    – Lundin
    Jun 14, 2021 at 14:13
  • \$\begingroup\$ I think the sensor needs a filter on the DCDC output or an LDO for say 10V as it works down to 6V and a 2V drop will minimize LDO heat. \$\endgroup\$ Jun 14, 2021 at 14:23

4 Answers 4

1
\$\begingroup\$

I've had similar problems with similar speed sensors on trucks. The problem might be the cable itself acting as antenna and picking up radiated noise. Check the signal with a scope, are you picking up stray spikes etc along with the pulse train?

The first thing I would try is to replace the cable with a shielded version and then obviously also try to keep it away from noisy things like the alternator. Also there's the classic last EMC resort: ferrite beads.

Also please note that adding custom electronics which is powered while the vehicle is running requires type approval and mandatory 3rd party EMC testing. If you put on some Rasp Pi there and the bike is involved in an accident, you'll get in some serious trouble with the law.

\$\endgroup\$
4
  • \$\begingroup\$ Even when I remove all the wiring so the sensor is no longer connected to the GPIO and I supply the Raspberry , the GPIO pin still detects interruptions. is that normal ? I am working on a Raspberry Pi 4 and using the WiringPi library. \$\endgroup\$
    – mina
    Jun 22, 2021 at 9:59
  • \$\begingroup\$ @mina If it's an input and you left it open, then it will behave randomly. You need to use pull resistors. \$\endgroup\$
    – Lundin
    Jun 22, 2021 at 12:43
  • \$\begingroup\$ I added a pull up and configured my GPIO pin also as a pull up and still detecting noise. It is because of the wires that I am using it is acting like an antennae. I tried to remove them and the gpio stopped detecting noise. I have to test with shielded cables but they are not available in my country :/ \$\endgroup\$
    – mina
    Jun 24, 2021 at 13:50
  • \$\begingroup\$ @mina, do you have access to a scope? It would be useful to see the signals from the sensor and the input to the GPIO. As a test, create a shield by putting the sensor wire in a metal pipe or wrap it in metal foil, and then ground both ends of the shield. \$\endgroup\$
    – tim
    Jun 25, 2021 at 17:14
1
\$\begingroup\$

That sensor has an NPN output. That means, an NPN transistor pulls the output down if the sensor is triggered, otherwise the output is disconnected.

This is your current circuit.

schematic

simulate this circuit – Schematic created using CircuitLab

The lowpass filter is between the internal 100k pullup in the Raspberry Pi, and your capacitor. Your resistor actually diminishes the filter effect as it limits the current the NPN transistor can draw from the capacitor. Remove it. If you want to increase the filter effect, add another 1kΩ pullup resistor to +3.3V in parallel to the internal 100kΩ resistor instead. That way the capacitor is recharged quickly after it was discharged by the NPN transistor and spikes cannot trigger the Raspberry input any more.

\$\endgroup\$
6
  • \$\begingroup\$ I have some doubts that it is a simple open-collector output after watching this video: youtube.com/watch?v=Vc0LKeh_0FQ \$\endgroup\$
    – ErikR
    Jun 14, 2021 at 14:42
  • \$\begingroup\$ I rather stick to the manufacturer's datasheet. \$\endgroup\$
    – Janka
    Jun 14, 2021 at 17:25
  • \$\begingroup\$ Perhaps, but the "datasheet" is rather skimpy on specifics and there's a lot of anecdotal evidence that the output will be either V+ or GND -- not an open collector output. We'll see -- I just ordered some from Amazon. \$\endgroup\$
    – ErikR
    Jun 14, 2021 at 17:32
  • \$\begingroup\$ I removed the resistor, added a pull up resistor. and now if I place the sensor 50 cm from the motorcycle chassis no pulses are detected unlike previously. on the other hand if it is closer to the motorcycle chassis or to a steel element it will start to detect several pulses. and when I tried to place a permanent magnet instead of detecting a single pulse it detects almost 10, it is hypersensitive. \$\endgroup\$
    – mina
    Jun 15, 2021 at 12:50
  • \$\begingroup\$ The sparks from the ignition of the bike mess around with all electronics nearby. Put the Raspberry Pi and your circuit in a metal casing that is connected with a conductor to the metal frame of the bike. Likewise, use a metal-fabric sleeve on the sensor cabling and connect it to the frame of the bike. Have your signal wires always stick tight to the ground wires or metal casing. \$\endgroup\$
    – Janka
    Jun 15, 2021 at 20:30
1
\$\begingroup\$

Here's information I found on the sensor:

The interesting tidbit in the SE question is:

The NJK-5002c doesn't have a lot of documentation, but I've found that if I connect the brown wire to V+ (12V), and blue wire to V- then the black wire will toggle between V- and V+ when a magnet comes close to the sensor.

But the fan demo at the 2:35 mark in the Arduino demo suggests an open-collector type output.

Without being able to inspect or experiment with the sensor itself, I would suggest the opto-coupler circuit below:

schematic

simulate this circuit – Schematic created using CircuitLab

Note that V+ is the power to the sensor and has to be at least 5V.

The circuit uses an optocoupler so that the Raspberry Pi power supply can be isolated from the sensor supply.

This configuration should give you clean square waves on the GPIO pin.

\$\endgroup\$
6
  • \$\begingroup\$ the blue wire of the sensor should be connected to the ground of the battery ? I didn't work the GPIO is still detecting interruptions when the engine is running. I used the optocoupler 4n25. I didn't connect the base . I forgot to add the optocoupler to the image but I was using but didn't do the same wiring, the one I was doing has no sense so I changed the wiring as you proposed an still didn't solve the problem \$\endgroup\$
    – mina
    Jun 22, 2021 at 9:49
  • \$\begingroup\$ When testing this when the engine is not running every thing works fine but if the engine is running it detects many interruptions. Even if I remove all the wiring so the sensor is no longer connected to the GPIO pin and supply the Raspberry Pi, interruptions will be detected I don't know how \$\endgroup\$
    – mina
    Jun 22, 2021 at 10:02
  • \$\begingroup\$ How is your Raspberry Pi being powered? \$\endgroup\$
    – ErikR
    Jun 22, 2021 at 14:02
  • \$\begingroup\$ even if I power my raspberry with the official power supply just t do the test, and I am choosing a Pull up GPIO and adding an external 10k pull up resistor It still detects noise. I thing that the wires are acting like an antenna \$\endgroup\$
    – mina
    Jun 24, 2021 at 13:47
  • \$\begingroup\$ @JRE - I'm not sure what you're getting at -- I marked one with "3.3V ground", and I mentioned that the RPi supply "can be isolated from the sensor supply." \$\endgroup\$
    – ErikR
    Jun 25, 2021 at 14:45
0
\$\begingroup\$

The NJK-5002C can sink up to 150 mA, so the 100 Ω resistor provides a strong pull-up to 12 V. The sensor transistor provides a strong pull-down to ground sinking 120 mA when the magnet passes by the sensor. That should provide a high degree of noise immunity.

Next, the voltage is stepped down to about 3.2 V using a voltage divider and then low pass filtered for a cut-off frequency of 14 Hz. It can work with a duty cycle of about 5% to 100%.

Finally, a Schmitt trigger reshapes the signal to produce clean edges for the GPIO interrupt pin. The low threshold is 1.1 V and the high threshold is 2.2 V Either inverting or non-inverting can be used.

You may have to adjust the component values and Schmitt trigger threshold levels if you want to work with a duty cycle of less than 5%.


Click to simulate.

Falstad simulation

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.