SNCY
Posts: 1
Joined: Sat Sep 15, 2018 2:38 am

wiringPi digitalWrite not working on certain pins

Sat Sep 15, 2018 2:48 am

Hi everyone,

I've been ripping my hair out (and putting lots of google hours in without success) over this issue for quite some time now...

I have some C/C++ code that, among other things actuates two relays that are on pin 0 and pin 1 (wiringPi pins, so on pins 11 and 12 on the header that is). Also, I have a PWM output on pin 26. I am running it on a Pi Zero.

The strange thing is, that if I do it with the command line tool gpio, everything seems to work fine. However when I run my C code, the relays don't switch on pins 0 and 1. It looks like the pins are still in input mode.
The PWM however works like a charm.

I attached a little example code here to demonstrate the problem. I found a workaround by calling gpio over system calls, but that violates my sense of decency...

I appreciate all of your help! Thanks in advance!

Code: Select all

#include <wiringPi.h>
#include <iostream>
#include <stdio.h>
#include "LiquidCrystal/LiquidCrystal_I2C.h"

void PINOP(const char* cmd, int pin, int val)																
{																						
	char sysBuffer[256] = "";															
	snprintf(sysBuffer, sizeof(sysBuffer), "gpio %s %d %d", cmd, pin, val);				
	system(sysBuffer);
}

int main(void)
{
	int pin0 = 1;
	int pin1 = 0;

	LiquidCrystal_I2C LCD("/dev/i2c-1", 0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
	LCD.begin(20, 4);
	LCD.clear();
	LCD.setBacklight(255);
	LCD.noAutoscroll();

	wiringPiSetup();
	pinMode(0, OUTPUT);
	pinMode(1, OUTPUT);
	pinMode(26, PWM_OUTPUT);

	//system("gpio mode 0 out");
	//system("gpio mode 1 out");
	//system("gpio mode 26 pwm");

	for (int i = 1023; i >= 0; i-=10)
	{
		//PINOP("write", 0, pin0);
		//PINOP("write", 0, pin1);
		
		digitalWrite(0, pin0);
		digitalWrite(1, pin1);

		LCD.printf("Pin0: %d\nPin1: %d\nPWM Level: %d", pin0, pin1, i);

		pwmWrite(26, i);
		//PINOP("pwm", 26, i);
		pin0 = pin0 ? 0 : 1;
		pin1 = pin1 ? 0 : 1;

		//LCD.printf("PWM Level:\n%d\n", i);
		LCD.setBacklight(i);
		std::this_thread::sleep_for(std::chrono::seconds(1));

	}

	return 0;
}

Return to “C/C++”