I've got 2 motors, 2 encoders, a motor controller, and my B+ wired up. My B+ is running Arch with kernel: Linux drd 3.12.35-1-ARCH #1 PREEMPT Tue Dec 23 07:14:51 MST 2014 armv6l GNU/Linux.

The 2 encoders each are an 8-pole magnet and hall effect sensor. I've got the face of each sensor 1-2mm away from the edge of the magnetic disc.

I am having issues using the BCM2835 library with event detection (falling edge, rising edge, level, or asynchronous falling/rising), like bcm2835_gpio_ren(pin). With simple pin polling using bcm2835_gpio_lev(pin), counting the encoder ticks works just fine.

When trying to use event detection, I get very little in the way of events. Here's some of the code:

//poll encoder, returning 1 if the state has switched, 0 otherwise
uint8_t encoderTick(uint8_t pin, uint8_t last)
    //return (bcm2835_gpio_lev(pin) != last);
    if (bcm2835_gpio_eds(pin))
        return 1;

    return 0;

It's currently very similar to the event example from the library documentation. I would ideally like to switch level polling with rising and/or falling edge detection (if possible to do both), but neither one seems to work. With level polling, I get the 8 events per rotation, but it is highly erratic at certain main loop and motor speeds (it depends too much on when I do the actual polling). With event detection, it seems I only get 0-3 events per 10 rotations of the motor. What's the deal?

In summary:

  • Why is event detection not working?
  • Can I do both rising and falling edge detection at the same time?

I found this thread which describes a sort of similar situation, but it was no help as I am running a newer kernel, and my library code does not freeze.

  • You give no idea of the RPM. Have a look at my PWM test. The motor was doing 9000 RPM.
    – joan
    Jan 10, 2015 at 20:15
  • 10~400 wheel RPM, where the encoders are. Which is 500~18,000 (!?) motor RPM.
    – Nick
    Jan 10, 2015 at 20:30
  • I'm not sure how many events per second you need to cope with. 400 * 8 * 2 (edges) * 2 (motors) / 60 suggests only 213. That can't be right, that's insignificant on the Pi. Could you clarify the events per second?
    – joan
    Jan 10, 2015 at 20:49
  • So are you suggesting I should just use the polling since it works? You're more or less correct. It's 400 RPM * 8 poles * 2 edges / 60 = 106 ticks/s for one motor. That's assuming I can check on both edges. Sorry if it wasn't clear. My question is more of: Why isn't event detection working, and can I do both rising and falling edge detection? I've edited the question to reflect.
    – Nick
    Jan 10, 2015 at 23:49
  • If you were in the 20 thousand events per second region I could have understood bcm2835 edge detection not working. At the number of events per second you are talking about it is inexplicable. If something else is hammering the CPU I suppose that could explain it. As to getting it working it doesn't seem like you have much of a choice.
    – joan
    Jan 10, 2015 at 23:58

1 Answer 1


The example in the documentation is only suitable for one time event detection. The better way to do it:

void waitForEncoderTick(uint8_t pin)

bcm2835_gpio_set_eds(pin);//clear the previous event. makes sure that only next event is detected
while(!bcm2835_gpio_eds(pin));//waits until next event/tick


It is also dependent on what you do between polling. If you do some heavy processing, it could happen that the sensor isn't polled in time.

Yes you could enable two events with corresponding functions, and both will set the event register.

Your Answer

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

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