Testing CNC Code Speed with Raspberry Pi

Testing CNC Code Speed with Raspberry Pi

C, Java, and Python code were used in separate tests to see how fast a Pi can toggle a digital output pin.

Download this article in PDF format.

A Raspberry Pi can be found centric to 3D printers and other CNC machines—almost  since the sale of the first Raspberry Pi. Wired had an article about it back in 2013! But can a Pi cut it as a CNC brain? How fast can a Raspberry Pi toggle a digital output pin? A Pi (depending on the model) ticks along at near or just above the 1-GHz mark. So, off the cuff, you can bet it is really, really fast. The problem with that particular assessment is “really, really fast” is not an exact unit of measure.

Boomerangs and magic bullet blenders also go super-fast, so are they all going at the same speed? There is only one way to find out. Test’em, so we know what’s fastest—blenders, boomerangs, or Raspberry Pis. Just kidding, we will just test the Raspberry Pi with a few different types of code to measure the pin toggle speed.

The Method

Only two essential pieces of equipment were required for testing. A Raspberry Pi of course (in this case a Pi 3) and an oscilloscope to measure and observe what was going on.

It was decided that the Pi would be tested with C, Java, and Python. The code, regardless of the language, is all very similar to what you might otherwise use to blink an LED. The only difference is the lack of a delay between switching a pin HIGH or LOW. In fact, an LED and a resistor were used as a visual indicator while writing the code (Fig. 1).

 

Python was a bit faster to get working than its Java and C counterparts. To control the Raspberry Pi’s pins, it employs the “RPi.GPIO” module. This module is already part of the Python installation on the RasPi, so there weren’t any extra libraries that needed to be installed.

 

Perhaps the fastest way to write and run C code on the RasPi is within the Geany IDE that comes with the Raspbian, which is where the C toggle code was created. The code uses the bcm2835 library for IO control. If you want to try this for yourself, you might have to follow the link and install it.

 

The Java code test used the BlueJ IDE. Before the JAVA code can be executed, the PiJ4 library must be installed. It may already come with the BlueJ IDE, but in case you don’t have it, follow the link and install it on your Pi.

The Results

Each code was run in the absence of any other user-instigated processes. The only things running are the code and its associated IDE. The results were witnessed and captured on the oscilloscope.

1. The wiring scheme including an LED and resistor as visual indicators.

The fastest that Python 3 could manage is shown in Figure 2.  A period of about 3.2 µs can be observed. This comes out to a frequency of about 312500 Hz.

 

2. Here’s the result of Python 3 code toggling as fast as it could. Notice the textbook ringing on the rising and falling edges.

The JAVA code’s best effort is shown in Figure 3.  It is considerably faster than Python with a period of about 600 ns. That’s a frequency of approximately 1.66 MHz.

3. Java is toggling as fast as it could in this test.

By far, the fastest code was the Geany C, with a scorching 100-ns period (Fig. 4). That’s a frequency of 10 MHz. However, look at that waveform—it’s a bit of a mess. The hard angles on the waveform mean that we were hitting the upper limit of the oscilloscope’s ability to sample a signal without loss.

4. This is the C code running from the Geany IDE. That waveform is less than ideal.

In an attempt to make the C code output a better-looking wave, a small, makeshift delay was inserted into the code. A bit of experimenting seemed to show that a stretching the period out to about 225 ns (≈4.4 MHz) made the wave more “square” (Fig. 5).

 

5. This is the same C code as Fig. 4, but it has been modified with a small makeshift delay.

Conclusion

Though the approximate upper-speed limit of 4.4 MHz might be faster than anything most people will ever need, it does answer the question of how quickly a Pi pin can toggle. Streaming rapid-fire step pulses out to a motor driver or setting up a 5-Mb/s I2C bus probably won’t be a problem with your Raspberry Pi. You will might run out of pins before you run out of available pulses per second. Of course, these tests were done with tiny programs devoid of any other tasks. Once you start adding functions and interrupt calls, the available time might start to rapidly diminish.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish