So i'm having this function (code below) in c that checks data from my database. If this data equals 255 i want to have a LED to light on one of my GPIO pins (12) on the raspberry pi.

I first tried the code WITHOUT bcm functions, it worked all fine. Now that I have the bcm2835 included, and changed my Makefile (compiling worked fine), I'm getting this "Segmentation fault" error.

I know this means my program is using memory it shouldn't be using, but I have no idea what causes this in the bcm lines.

Here is the funtion:

void check_pasid(char k[]){

MYSQL *conn;
MYSQL_RES * result;

   char *server = "server";
   char *user = "myusername";
   char *password = "mypassword";
   char *database = "dbname";
   char query1[100];

   // Make the connection to the Mysql-database.
   conn = mysql_init(NULL);

   if (!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0)) {
      fprintf(stderr, "%s\n", mysql_error(conn));


sprintf(query1,"SELECT COUNT(*) FROM passcan WHERE UserID = \"%s\";",k);

int result1 = mysql_query(conn, query1);
result = mysql_store_result(conn);
row = mysql_fetch_row(result);

int compare = 1;
compare = strcmp(row[0], "1");

if(compare == 0){
    printf("Led is turning on");
    // Turn it on
    bcm2835_gpio_write(PIN12, HIGH); 
    // Turn it off
    bcm2835_gpio_write(PIN12, LOW);
    printf("Led is not turning on");


I hope someone has some information for me.

  • 2
    This question appears to be off-topic because it is primarily about a programming issue. Stack Overflow would be a better fit for this question.
    – syb0rg
    Jan 14, 2014 at 15:44
  • Ah i see, you're right, wrong site @syb0rg Jan 14, 2014 at 15:46

1 Answer 1


I cannot see a call to bcm2835_init() which must be called before calling any other functions in the bcm2835 library.

Also ensure you run your program with root access, since this is needed to use the bcm2835 library.

  • The suggestion to run with root access saved me a lot. +1
    – stardust
    Jul 11, 2020 at 23:07
  • I would just add that you should execute bcm2835_close() when finished interfacing. I've also gotten segmentation faults executing more than one bcm2835_init() without first closing with bcm2835_close().
    – gorlux
    Feb 10, 2021 at 0:55

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.