Connect with us

ardino code for Two color LED to PIC18F45K80

Discussion in 'Microcontrollers, Programming and IoT' started by Djsarkar, Oct 9, 2020.

Scroll to continue with content
  1. Djsarkar

    Djsarkar

    33
    1
    Jul 27, 2020
    Hi,

    I am looking help to understand following code because I do not understand programming for Arduino

    https://tkkrlab.nl/wiki/Arduino_KY-029_Yin_Yi_2-color_LED_module_3MM

    I have PIC18F45K80. I am using MPLABX 5.40 and XC8 2.30

    Code:
    // Arduino test code:
    int redpin = 11; // select the pin for the red LED
    int bluepin = 10; // select the pin for the blueLED
    int val;
     
    void setup () {
      pinMode (redpin, OUTPUT);
      pinMode (bluepin, OUTPUT);
      Serial.begin (9600);
    }
     
    void loop () {
     for(val=255;val>0;val--) {
       analogWrite (11, val);
       analogWrite (10, 255 - val);
       delay(5);
     }
     for(val=255;val<255;val++) {
        analogWrite (11, val);
        analogWrite (10, 255 - val);
       delay(5);
     }
    }
    There are two for loop in code. I do not understand What's happening inside For loop for Two color LED's?

    Thanks for any help
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    10,342
    2,247
    Nov 17, 2011
    See here:
    analogWrite()
    delay()

    Arduino code is a dialect of C++, easy to understand.
     
  3. Djsarkar

    Djsarkar

    33
    1
    Jul 27, 2020
    I don't understand what's happening in inside loop for LED controlling. LED can give red or green color at a time. I am trying to understand what's the logic needed to get different color from LED

    simple I want a blue light on /off for 2 seconds and red light on /off for 2 seconds repeat process. Can you tell me logic for this ?

    my plan for different color

    BLUE_Pin = ON
    RED_Pin = OFF
    Wait for 2 seconds
    BLUE_Pin = OFF
    RED_Pin = ON
    Wait for 2 seconds
    Repeat process ..

    Is my plan okay if its then I will write code ?
     
  4. Harald Kapp

    Harald Kapp Moderator Moderator

    10,342
    2,247
    Nov 17, 2011
    AnalogWrite() can output an analog value between 0 V (data = 0) and 5 V (data = 255) any value in between will make the LED shine more or less bright. If you simply need on/off, replace analogWrite by a digital output of 0 or 1 to the output pin the LED is connected.
    Your list shows the logic.

    Why do you keep looking up Arduino Code if you don't understand it and can't use it? Search for PIC code instead, that'll help you much more.
     
    Djsarkar likes this.
  5. Djsarkar

    Djsarkar

    33
    1
    Jul 27, 2020
    @Harald Kapp Do i need to use current limiting resistor ?

    Edit : spelling error
     
    Last edited: Oct 9, 2020
  6. Harald Kapp

    Harald Kapp Moderator Moderator

    10,342
    2,247
    Nov 17, 2011
    What is a "current limiting register"?
    Do you mean current limiting resistor? Yes, you do. See here.
     
    Djsarkar likes this.
  7. bertus

    bertus Moderator

    1,000
    371
    Nov 8, 2019
    Djsarkar likes this.
  8. Djsarkar

    Djsarkar

    33
    1
    Jul 27, 2020
    Here is my code Big LED doesn't show any light only small led is blinking
    Code:
    #define _XTAL_FREQ 8000000
    #include <xc.h>
    // PIC18F45K80 Configuration Bit Settings
    // 'C' source line config statements
    // CONFIG1L
    #pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
    #pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
    #pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
    #pragma config XINST = OFF       // Extended Instruction Set (Enabled)
    // CONFIG1H
    #pragma config FOSC = INTIO2    // Oscillator (Internal RC oscillator)
    #pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
    #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
    #pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)
    // CONFIG2L
    #pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
    #pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
    #pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
    #pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)
    // CONFIG2H
    #pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
    #pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)
    // CONFIG3H
    #pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
    #pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
    #pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)
    // CONFIG4L
    #pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
    #pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)
    // CONFIG5L
    #pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
    #pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
    #pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
    #pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)
    // CONFIG5H
    #pragma config CPB = OFF        // Code Protect Boot (Disabled)
    #pragma config CPD = OFF        // Data EE Read Protect (Disabled)
    // CONFIG6L
    #pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
    #pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
    #pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
    #pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)
    // CONFIG6H
    #pragma config WRTC = OFF       // Config. Write Protect (Disabled)
    #pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
    #pragma config WRTD = OFF       // Data EE Write Protect (Disabled)
    // CONFIG7L
    #pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
    #pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
    #pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
    #pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)
    // CONFIG7H
    #pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)
    
    #define RED            LATBbits.LATB0
    #define BLUE            LATBbits.LATB1
    
    void Port_Initialized (void);
    
    void main(void)
    {
     
        Port_Initialized ();
     
        while (1)
        {
             LATBbits.LATB0 = 0;  LATBbits.LATB1 = 1;
              __delay_ms(2000);
             LATBbits.LATB0 = 1;  LATBbits.LATB1 = 0;
              __delay_ms(2000);
          
        }
    }
    
    void Port_Initialized (void)
    {
        LATA = LATB = LATC = LATD = LATE =  0;
        TRISA = 0b0000000;// all are output, Unused
        TRISB = 0b0000000;//
        TRISC = 0b0000000;// all are output, Unused
        TRISD = 0b0000000;//
        TRISE = 0b0000000;// All are output, Unused
     
        ANCON0 = 0; // digital port
        ANCON1 = 0; // digital port
        CM1CON = 0; // Comparator off
        CM2CON = 0; // Comparator off
        ADCON0 = 0; // A/D conversion Disabled
    }
      
    
    
    IMG_20201009_220358.jpg
     
  9. Djsarkar

    Djsarkar

    33
    1
    Jul 27, 2020
    Can anyone guess what may be problem so LED is not working ?
     
  10. bertus

    bertus Moderator

    1,000
    371
    Nov 8, 2019
    Hello,

    Have you tested the leds connections?
    Do you know wich pin is the common pin?
    Do you know if the led is common cathode or common anode?

    I found this page, where the left pin seems to be the common pin, the middle pin for red and the right pin for green:
    https://diyi0t.com/led-tutorial-for-arduino-and-esp8266/

    Bertus
     
    Djsarkar likes this.
  11. Djsarkar

    Djsarkar

    33
    1
    Jul 27, 2020
  12. bertus

    bertus Moderator

    1,000
    371
    Nov 8, 2019
    Hello,

    Where are the leads going to?
    I only see the module and not the other connections.
    To test the module, connect the left pin to ground and the middle pin to +5 Volts.
    The red led should be lit.
    The other test is the left pin to ground and the right pin to + 5 Volts.
    The green led should be lit.
    When the middle and right pins are both connected to + 5 Volts, both leds will be on and the led will shine an orange like color.

    Bertus
     
  13. Djsarkar

    Djsarkar

    33
    1
    Jul 27, 2020
    I have done as you said but no luck

    Please look at attached screenshot. Yellow connected to ground
     

    Attached Files:

  14. bertus

    bertus Moderator

    1,000
    371
    Nov 8, 2019
    Djsarkar likes this.
  15. Djsarkar

    Djsarkar

    33
    1
    Jul 27, 2020
    Its difficult to find which one I have, because nothing is written on the board

    Back to post #1 I think I should try the example code of Arduino But I do not understand the Arduino program. I have searched the sample code for the pic which I am not getting on internet.

    If someone can tell me the logic of Arduino program, then I can write program for pic
     
  16. bertus

    bertus Moderator

    1,000
    371
    Nov 8, 2019
    Hello,

    The arduino code is fading from red to green and from green to red and so on.
    The delay between each fade step is 5 mSec.
    The total cyclus is 2 times 256 steps of 5 mSec.
    So the total cyclus is about 2.5 seconds.

    Bertus
     
  17. bertus

    bertus Moderator

    1,000
    371
    Nov 8, 2019
    Hello,

    I am thinking, perhaps you module uses an other kind of led and does not follow the connections of the arduino modules.

    Bicolor_LED_cross_section_three_pin_version 600px.jpg

    Try the middle pin to ground and the left or right via a 220 Ohms resistor to the + 5 Volts.

    Bertus
     
    Djsarkar likes this.
  18. Djsarkar

    Djsarkar

    33
    1
    Jul 27, 2020
    I did the connection as you said. Red light is turning on.
     

    Attached Files:

  19. bertus

    bertus Moderator

    1,000
    371
    Nov 8, 2019
    Hello,

    Then the right pin should light the green led.

    Bertus
     
  20. Djsarkar

    Djsarkar

    33
    1
    Jul 27, 2020
    No green light does not turn on
    I did middle pin to ground and the right via a 300 Ohms resistor to the + 5 Volts.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-