Lets Make Tech

Controlling The LEDs On A Beaglebone Black With A Bash Script

Learning to do this was what taught me how to use GPIOs and jump started my electronics hobby. 

Steps:

1. Open a terminal on your Beaglebone, login as root using the su command. 

su

2. Start nano text editor by typing 

nano LED.sh

3. Type the header. The first line is important, the second is a comment. 

#!/bin/bash
# flasher (this is a comment I am just saying what the program will do you can type anything or exclude this)

4. Tell it where the LEDs are. This is Linux, everything is a file and located somewhere. 

cd /sys/devices/ocp.3/gpio-leds.8/leds

5. Make the system info flashing (heartbeat) stop. Note there are four LEDs, usr 0-3. You can navigate to each LED's folder and look around if you want.

echo none>beaglebone:green:usr0/trigger
echo none>beaglebone:green:usr1/trigger
echo none>beaglebone:green:usr2/trigger
echo none>beaglebone:green:usr3/trigger

6. Now we are going to make an endless loop because we are awful people like that. 

while true
do

echo 0 > beaglebone:green:usr0/brightness
echo 0 > beaglebone:green:usr1/brightness
echo 0 > beaglebone:green:usr2/brightness
echo 0 > beaglebone:green:usr3/brightness

sleep 1

echo 1 > beaglebone:green:usr0/brightness
echo 1 > beaglebone:green:usr1/brightness
echo 1 > beaglebone:green:usr2/brightness
echo 1 > beaglebone:green:usr3/brightness

sleep 1

done

7. Save and exit by hitting ctrl+o then ctrl+x

8. It isn't ready to run yet, first you need to make it executable with the following command:

chmod +x LED.sh

9. Run it.

./LED.sh

10. It is an infinite loop... but it has to end at some point. Hit ctrl+c to kill the process.

11. Make your Beaglebone's heartbeat again (flash lights to let you know it is doing things) 

echo heartbeat>beaglebone:green:usr0/trigger
echo heartbeat>beaglebone:green:usr1/trigger
echo heartbeat>beaglebone:green:usr2/trigger
echo heartbeat>beaglebone:green:usr3/trigger

 12. Try other things

echo timer>beaglebone:green:usr0/trigger
echo timer>beaglebone:green:usr1/trigger
echo timer>beaglebone:green:usr2/trigger
echo timer>beaglebone:green:usr3/trigger
echo 100>beaglebone:green:usr0/delay_on
echo 100>beaglebone:green:usr0/delay_off

echo 100>beaglebone:green:usr1/delay_on
echo 100>beaglebone:green:usr1/delay_off

echo 100>beaglebone:green:usr2/delay_on
echo 100>beaglebone:green:usr2/delay_off

echo 100>beaglebone:green:usr3/delay_on
echo 100>beaglebone:green:usr3/delay_off

You can control the GPIOs pretty much the same way, they are located in the GPIO folder instead of the leds folder. Now you can do anything. 

 

 

 

How To Set Up A PIC 18F4520

First you need to make sure you have everything that you need.

BOM

-1x PIC18f4520

-PICkit-3

-Breadboard

-Jumper wires

-5V power regulator

-9V battery and clip. 

-20 Mhz oscillator

-100 kohh resistor

-2X 22pf capacitors

- 2X 10uf capacitors 

 

Connections

1. First set up your bread board by placing the 5V regulator somewhere on it. Connect the ground pin to the ground rail of the breadboard        and the out pin to the + rail of the breadboard. Connect the 9V positive pin to the voltage regulator input and connect ground to ground. 

2. Check the rails with a multimeter to make sure that you are getting a 5V reading. Once you confirm it is working disconnect the battery.  

3. Now place your pic somewhere else on the bread board. Connect pins 11 and 32 to the positive power rail (5V). Connect pins 12 and 31 to     the ground rail. Place a 10uf capacitor between pins 11 and 12 as well as between pins 31 and 32 (note the polarity of the capacitor when       connecting)

4. Connect a 100 kohm resistor from pin 1 to the 5V rail. 

5. Connect the 20 MHz oscillator to pins 13 and 14. Add one 22pf capacitor from pin 13 to ground and another 22pf capacitor from pin 14 to     ground.  

6. Now its time to connect to your computer with the PICkit 3. Pin 1 of the PICkit 3 designated by a white arrow connects to pin 1 of the PIC.     Pin 2 of the PICkit 3 connects to 5V. Pin 3 of the PICkit-3 connects to ground. Pin 4 of the PICkit-3 connects to pin 40 of the PIC. Pin 5 of       the PICkit-3 connects to Pin 39 of the Pic

7. Configure your PIC: Go to the 'Window' tab, select 'Pic Memory Views'. Configure how you need and get the code, mine looks like this:

// CONFIG1H
#pragma config OSC = HS         // Oscillator Selection bits (HS oscillator)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = OFF     // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)

 

8. Now you just have to write a program. Here is an example of code that uses an EMG sensor to control a stepper motor. If you are interested in building this check out my previous post

 


// PIC18F4520 Configuration Bit Settings

// 'C' source line config statements

#include <xc.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

// CONFIG1H
#pragma config OSC = HS         // Oscillator Selection bits (HS oscillator)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = OFF     // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)

void setup();
void read();
void motorcontrol();
void LCD();
void forward();
void reverse();
void pause();
int adresult = 0;
int steps=0;
float voltage = 0.0;
int newstep;
void main(void)
{
    setup();
    while(1)
    {
        read();
        motorcontrol();
        LCD();
    }
}

void setup()
{
    TRISC = 0b00000000;
    LATC = 0b00000000;
    ADCON0 = 0b00000001;
    ADCON1 = 0b00001110;
    ADCON2 = 0b00100100;
}

void read()
{
    ADCON0bits.GO = 1;
    while (ADCON0bits.GO == 1);
    adresult = ADRESH;
    voltage = adresult * 5.0 / 255; //8bits 0-255 so 255 steps.
    newstep = voltage*200.0;
}

void motorcontrol()
{
    if (steps<newstep && newstep>50)
    {
        forward();
        steps++;
    }
    if (steps>newstep && newstep < 250)
    {
        reverse();
        steps--;
    }
}

void LCD()
{
    
}

void forward()
{
    LATC = 0b00001001;
    pause();
    LATC = 0b00000011;
    pause();
    LATC = 0b00000110;
    pause();
    LATC = 0b00001100;
    pause();
    LATC = 0b00000000;
}

void reverse()
{
    LATC = 0b00001100;
    pause();
    LATC = 0b00000110;
    pause();
    LATC = 0b00000011;
    pause();
    LATC = 0b00001001;
    pause();
    LATC = 0b00000000;
}

void pause()
{

   _delay(12350); //15000 fastest 100000 slowest
}




 

Newer posts → Home