# Cannot get a digital square wave on oscilloscope from the GPIO of the Raspberry Pi?

ENVIRONMENT:

Running "Raspbian" on Model_B Raspberry Pi (Revision 2) w/ ribbon expanding the GPIO out onto a Breadboard as shown in the following image.

SETUP

Here is a close up of the GPIO setup on the breadboard and the connection of my probe. My probe is connected to the Green wire which is connected to pin 7 (CE1). My ground is obviously connected to ground which is the yellow wire.

I'm using GPIO pin 7 (CE1) as an output which I have my probe connected to and my ground is connected to the ground pin. I've set probe on 10:1 setting.

My program reflects the example for the "C" "wiringPi library" program located here . You will need to scroll down as the example is located towards the end. I'm also attaching a image below of my program.

I'm unsure what the (if) statement is doing but as you can see i'm setting the pinMode to (7,OUTPUT) and using a (while) loop to toggle pin on and off as fast as possible. I'm hoping to achieve a digital square wave from this program to view on my oscilloscope. However, i'm getting a bad signal on the screen as you can see below.

THE PROBLEM I'M HAVING:

Can someone help me troubleshoot my setup so I can get a digital square wave to display. The link I attached above gives an article on how they set their test bench up. The even show the signal that was displayed on the oscilloscope. However, for some reason I cannot replicate their results. Below is an article for the spec sheet of the GPIO. Again my board is revision 2.

SPEC SHEET FOR THE GPIO:

PROBLEM HAS BEEN RESOLVED:

Thanks to K-Sid's comment below and the wonderful link I have fixed the issue. I'm posting the new image of the signal being analyzed. It's showing a frequency of 4.3MHz which is around what it should be.

• That doesn't seem to match the example and what I can see would just leave the line low the whole time, try changing one of the lines to (4,1) and see what happens. You could also try the shell script for comparison. Also I think they should be (7,1) and (7,0) – PeterJ Feb 26 '14 at 4:05
• It's possible that raspberrypi.stackexchange.com may be of interest to you. (It seems to me lately that the StackExchange community is trying to split topics like this up.) – JYelton Feb 26 '14 at 4:07
• @PeterJ I tried changing it digitalWrite(4,0) then digitalWrite(4,1). I changed my time divisions down to 10ms/div. I got a 62Hz signal but it looks like a sine wave. I then changed it to digitalWrite(7,0) then digtalWrite(7,1) and got the same exact thing. Next I created the shell script they had where the called the gpio command followed by 4,0 then 4,1. Its that shell script they have right under the C program I was doing. when I did that I got the same exact signal 60Hz that looked like a sine wave. So I'm assuming this is not the signal I should be getting and in other words is noise. – Shane Yost Feb 26 '14 at 4:58
• I haven't used the GPIO on a Pi myself but yes the 60Hz signal would be mains hum, so the line sounds like it's floating (probably not getting configured as an output properly). – PeterJ Feb 26 '14 at 5:01

There are a couple of issues with your code that can be easily fixed. First, you are calling wiringPiSetup which means you will need to refer to the pins by their WiringPi designation which can be found on this page:

https://projects.drogon.net/raspberry-pi/wiringpi/pins/

You are then setting pin 7 (wiringPi designation) to be an output which is actually GPIO 4 (physical pin 7) and not the CE1 pin as you mentioned. CE1 would actually be wiringPi pin 11.

The next problem is in the while loop. You are doing a digitalWrite on wiringPi pin 4 (not 7 as you configured) and are always setting it low so it would never toggle high even if configured correctly. The following code should be what you are looking for but is not tested. This assumes you want to use CE1 which is normally used for SPI but should work with WiringPi as an general I/O pin:

  if (wiringPiSetup () == -1)
exit (1) ;

pinMode(11, OUTPUT);

while(1) {
digitalWrite(11, 0);
digitalWrite(11, 1);
}

• Thanks this really helped out. I appreciate it. I'm going to post the new image of the signal i'm getting just to provide a good closing for this thread for anyone that might have the same issue. – Shane Yost Feb 26 '14 at 5:05
• Awesome. Glad you got it working. – K-Sid Feb 26 '14 at 6:04

While @K-Sid has solved your issue, there's one more thing. Your output:

is very noisy. That's because even though your code is giving you square clock, you don't have any control over the time period. It's always wise to add a delay:

  while(1) {
digitalWrite(11, 0);
delay(10); // Hold positive-logic LOW for 10ms.
digitalWrite(11, 1);
delay(10); // Hold positive-logic HIGH for 10ms
}


If you need square waves with higher frequencies, use clock generators (or crystals).

Hope this helps.