ccurran689
Posts: 13
Joined: Sat Sep 26, 2015 9:55 am

Controlling LED's using C language

Sat Sep 26, 2015 10:00 am

I have been trying to get this working for the past day with no avail; I am trying to make a LED blink (Simple I know)

I have tried BCM2835 and WiringPi Libraries

I can turn the LED on but once I add a delay function in BCM2835 it either does not light at all or will stay a steady light.

This is my most recent attempt in wiringPI, I first get into the directory where the file is; cd /home/pi/desktop
Then I compile using [ gcc -o wiringpi wiringpi.c -lwiringPi ]
and run using sudo ./wiringpi

this is the code below;

#include <stdio.h>
#include <wiringPi.h>


int main(void )
{
printf (" Eyoooo");
if (wiringPiSetup () == -1)
return 1;

pinMode (23, OUTPUT) ;

for (;;)
{
digitalWrite (23,1);
delay (5000);
digitalWrite (23,0);
delay (5000);
}

return 0;
}

User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

Re: Controlling LED's using C language

Sat Sep 26, 2015 6:01 pm

Hello,

In a terminal:- man 3 sleep
Google:- gcc Wall

Regards

ccurran689
Posts: 13
Joined: Sat Sep 26, 2015 9:55 am

Re: Controlling LED's using C language

Sat Sep 26, 2015 6:57 pm

Can you explain more what your trying to say?

martinmarty
Posts: 27
Joined: Mon Sep 22, 2014 1:03 am

Re: Controlling LED's using C language

Sat Sep 26, 2015 10:33 pm

He's saying to look at the man (manual) page for the sleep function. You can type the command he gave you into a terminal session on your RPi. It will output something like this: http://linux.die.net/man/3/sleep

The sleep function would do the same thing as the delay function you are already using from wiringPi.

And he is saying to add the -Wall argument to your compile command to turn on all warnings. You want to be able to see if there is anything the compiler or linker are trying to tell you.

Technically what you are doing will work, but don't you find it confusing to name your source code and the executable the same thing as the library you are using, with only the capitalization to differentiate? You could name your source file something like blink.c and then do:

cc -Wall blink.c -lwiringPi -o blink
sudo ./blink

BUT, like I said, your program should still work the way you have it named.

You did not share with us the output. Can you confirm that it actually prints "Eyoooo" when you run it?

What I would like to see is for you to add a couple more lines,

printf("ON\n"); after your digitalwrite 1

and

printf("OFF\n"); after your digitalwrite 0

That way you should see it going through its cycle on your screen every ten seconds or so. This way we will know the program is basically functioning the way you intended.

So even though I don't like your program name, I suspect your actual problem is in the mapping of hardware pins to the software.

Are you using a version of RPi that has the 40-pin GPIO header???

Your program refers to pin "23". Does that mean that you are trying to use the pin named "GPIO23" which is actually on hardware pin 16 of the 40-pin header? i.e. Is your LED connected to pin 16? If yes, please leave the hardware as-is and try changing your wiringpi program to say pin "4" for all three relevant statements.

See if that helps.

-Marty

User avatar
mad-hatter
Posts: 419
Joined: Sun Feb 26, 2012 2:58 pm
Location: By the UK seaside

Re: Controlling LED's using C language

Sun Sep 27, 2015 9:06 am

Hello,

Have a look here :-
http://wiringpi.com/examples/blink/

Regards

ccurran689
Posts: 13
Joined: Sat Sep 26, 2015 9:55 am

Re: Controlling LED's using C language

Sun Sep 27, 2015 11:42 am

Got it sorted thanks! didn't realize that wiringPi had its own pin numbering system

Return to “Beginners”