Friday, October 7, 2022

[SOLVED] Increasing GPIO switching speed

Issue

I'm working on a little project I wanted to see come to life using a NEOPIXEL WS2812B with a PI 4. I think I might be up against a hardware limitation and I'm curious if there's anyway around it. Heres the code I'm using

void send1()
{
    //T1H
    digitalWrite(DATA, HIGH);
    //usleep(0.85);
    //T1L
    digitalWrite(DATA, LOW);
    //usleep(0.4);
}

As you can see I've commented out the usleep stuff because I was having issues with the timing and I wanted to see the maximum switching speed I can get.

enter image description here

With that I get a switching speed of around 1.200us. The neopixels requires a 0.4 and 0.85us switching speed to work properly. Does anyone have any suggestions that can help get that number down? I'm just using a regular gpio pin. I'm rather new to playing around with the pi so I don't know all the tricks.

I did see this on pinout.xyz

The PWM0 output of GPIO 18 is particularly useful, in combination with some fast, direct memory access trickery, for driving devices with very specific timings. The WS2812 LEDs on the Unicorn HAT are a good example of this in action.

That seems hopeful since the LEDs I'm using are the same model. but it doesn't really say much more about the tricks.


Solution

Direct Answer

You can try any of the following gpio libraries on rpi,

Further Suggestion

It is not recommened to generate PWM signal directly from Raspberry Pi, because the linux kernel is not designed for real-time application and any other higher priority process could take over CPU from any running process, so basically you cannot make sure to have a constant PWM signal output. Probably NEOPIXEL LED will be flicking or changing color slightly in your case.

If you need better quality of PWM output, you are suggested to use a small I2C PWM module, e.g. many PCA9685-based boards. Your raspberry pi will control the module via I2C interface only when the value need to upadte, so your CPU are saved for your other applications.



Answered By - balun
Answer Checked By - Mary Flores (WPSolving Volunteer)