Unable to control GPIO from the code
Ask QuestionAsked 7 days agoActive 7 days agoViewed 24 times1
I am using raspberry pi 4b along with Freenova kit. I am trying to make the LED blink however it does not blink instead it is ON of the time. I have tried different code (C,Python,Java) but none of it seems to work. What am I doing wrong? below is my Java code that I execute in Processing. None of it works, why? I want Java code the most.
import processing.io.*; int ledPin = 17; //define ledPin boolean ledState = false; //define ledState void setup() { size(100, 100); frameRate(1); //set frame rate GPIO.pinMode(ledPin, GPIO.OUTPUT); //set the ledPin to output mode } void draw() { ledState = !ledState; if (ledState) { GPIO.digitalWrite(ledPin, GPIO.HIGH); //led on background(255, 0, 0); //set the fill color of led on } else { GPIO.digitalWrite(ledPin, GPIO.LOW); //led off background(102); //set the fill color of led off } }
Python code:
import RPi.GPIO as GPIO import time ledPin = 11 # define ledPin def setup(): GPIO.setmode(GPIO.BOARD) # use PHYSICAL GPIO Numbering GPIO.setup(ledPin, GPIO.OUT) # set the ledPin to OUTPUT mode GPIO.output(ledPin, GPIO.LOW) # make ledPin output LOW level print ('using pin%d'%ledPin) def loop(): while True: GPIO.output(ledPin, GPIO.HIGH) # make ledPin output HIGH level to turn on led print ('led turned on >>>') # print information on terminal time.sleep(1) # Wait for 1 second GPIO.output(ledPin, GPIO.LOW) # make ledPin output LOW level to turn off led print ('led turned off <<<') time.sleep(1) # Wait for 1 second def destroy(): GPIO.cleanup() # Release all GPIO if __name__ == '__main__': # Program entrance print ('Program is starting ... \n') setup() try: loop() except KeyboardInterrupt: # Press ctrl-c to end the program. destroy()
C code:
#include <wiringPi.h> #include <stdio.h> #define ledPin 0 //define the led pin number void main(void) { printf("Program is starting ... \n"); wiringPiSetup(); //Initialize wiringPi. pinMode(ledPin, OUTPUT);//Set the pin mode printf("Using pin%d\n",ledPin); //Output information on terminal while(1){ digitalWrite(ledPin, HIGH); //Make GPIO output HIGH level printf("led turned on >>>\n"); //Output information on terminal delay(1000); //Wait for 1 second digitalWrite(ledPin, LOW); //Make GPIO output LOW level printf("led turned off <<<\n"); //Output information on terminal delay(1000); //Wait for 1 second } }



pythonpi-4ledjavacshareedit follow close 2flagedited Nov 2 at 13:27asked Nov 2 at 12:06newbie322 bronze badges
- 1Post a photo of your wiring. – CoderMike Nov 2 at 12:11
- 1Wiring in photo looks correct. Is you ribbon cable round the right way at the Pi end. Post a photo including ribbon cable at both ends. – CoderMike Nov 2 at 12:27
- 1It is, otherwise the led would not be ON, it is not a problem with the wiring – newbie Nov 2 at 12:29
- 1The led could be connected to other power pins if the cable is wrong. – CoderMike Nov 2 at 12:30
- 1No, cable is not wrong, I have tried literally all combinations. it is not. – newbie Nov 2 at 12:34
- 1No harm in posting another photo to confirm. – CoderMike Nov 2 at 12:38
- 1What is “Freenova”, post URL. – Mats Karlsson Nov 2 at 16:11
1 Answer
Your code and breadboard wiring looks fine to me. Your Python code works on my Pi.
I suspect your ribbon cable is the wrong way round either at the Pi end or the breadboard end. Your T Cobbler board could also be faulty.


shareedit follow flag answered Nov 2 at 12:53CoderMike4,52711 gold badge77 silver badges1515 bronze badges
- 1I have added more pictures, on one it is on but doesn’t flash, on the other one it is not even on. Do you have raspberry pi 4b a swell? – newbie Nov 2 at 13:23
- 1Yes, this is a Pi4B. – CoderMike Nov 2 at 13:25
- 1Your 3rd photo (LED on) – the cable at the Pi end is wrong. – CoderMike Nov 2 at 13:29
- 1so I assume that on 2nd photo it is connected correctly (It is connected like that at the moment), why led doesn’t work at all then? – newbie Nov 2 at 13:30
- With the ribbon cable as in your 2nd photo – you may need to turn the LED around. They only work in one direction. – CoderMike Nov 2 at 13:30
- 2Thanks Mike for your help 🙂 – newbie Nov 2 at 13:37
- @CoderMike, your advice is very good to newbies. You remind me of my bad experience of frying two Rpi Sensor hat is a row. It is about what you suggested to check “Is your ribbon cable connected wrong way round?” I have been playing with ribbon cable for years, keyed or not keyed, factory made, custom made, DIYed, long or short, 4/10/26/40 pin one. So I was over confident when try to use a long ribbon cable to extend my sensor board from Rpi. It is only after I fried all my two sensor boards (I usually order two, one for spare, sort of “second source/pair/swap”). / to continue, … – tlfong01 8 mins ago
- I stupidishy wrongly thought that of course I know the meaning of the red coloured wire of the 40 pins wire, but I did realised that even the coloured red assures one pin is on the correct side, there is NO guarantee that the other 39 pins are in correct order, because I might have “twisted” the up/down position. Anyway, I learned my lesson. I placed the two fried sensor boards (very expensive to a poor hobbyist like me!) in my “mistake junk box”, to remind me to think twice before I leap. Just thinking aloud, sorry for all the uncorrected grammatical and spelling mistakes. – tlfong01 just now Edit
Categories: Uncategorized