1

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
    }
}

enter image description here enter image description here enter image description here

7
  • 1
    Post a photo of your wiring.
    – CoderMike
    Commented Nov 2, 2020 at 12:11
  • 1
    Wiring 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
    Commented Nov 2, 2020 at 12:27
  • 1
    It is, otherwise the led would not be ON, it is not a problem with the wiring
    – newbie
    Commented Nov 2, 2020 at 12:29
  • 1
    The led could be connected to other power pins if the cable is wrong.
    – CoderMike
    Commented Nov 2, 2020 at 12:30
  • 1
    No, cable is not wrong, I have tried literally all combinations. it is not.
    – newbie
    Commented Nov 2, 2020 at 12:34

1 Answer 1

2

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.

enter image description here

enter image description here

7
  • 1
    I 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
    Commented Nov 2, 2020 at 13:23
  • 1
    Yes, this is a Pi4B.
    – CoderMike
    Commented Nov 2, 2020 at 13:25
  • 1
    Your 3rd photo (LED on) - the cable at the Pi end is wrong.
    – CoderMike
    Commented Nov 2, 2020 at 13:29
  • 1
    so 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
    Commented Nov 2, 2020 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
    Commented Nov 2, 2020 at 13:30

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.