haylocki
Posts: 11
Joined: Tue Nov 11, 2014 7:24 am

Setting gpio pins on boot

Tue Nov 11, 2014 8:24 am

Hi, I recently bought a relay card similar to this :
http://www.ebay.co.uk/itm/8-Channel-5V- ... 417979a25d

And I was a bit miffed to discover that the Pi was turning on some of the relays during boot.

To solve this problem it is necessary to set the gpio pins during boot. Fortunately this has recently become possible by doing the following :

Update the firmware

Code: Select all

sudo rpi-update
And reboot when requested

Install device tree compiler and certificates

Code: Select all

sudo apt-get install ca-certificates device-tree-compiler
Create a dt-blob.dts file

Code: Select all

nano dt-blob.dts
add the following to the file:

Code: Select all

/dts-v1/;

/ {
   videocore {

// The following line is for a version 2 board
            pins_rev2 {
// For a version 1 board use the following line instead
//            pins_rev1 {
// For a B+ board use the following line instead
//            pins_bplus {

               pin_config {
                  pin@default {
                     polarity = "active_high";
                     termination = "pull_down";
                     startup_state = "inactive";
                     function = "input";
                  }; // pin

// I have rem'd out gpio pins 2 and 3 here, as I am using them for my relays
// If you're not using them you should enable them here
// On a version 1 board this will be pins 0 and 1
                  //pin@p2  { function = "i2c0";   termination = "pull_up"; }; // I2C 0 SDA
                  //pin@p3  { function = "i2c0";   termination = "pull_up"; }; // I2C 0 SCL

                  pin@p5  { function = "output"; termination = "pull_down"; }; // CAM_LED
                  pin@p6  { function = "output"; termination = "pull_down"; }; // LAN NRESET
                  pin@p14 { function = "uart0";  termination = "no_pulling"; drive_strength_mA = < 8 >; }; // TX uart0
                  pin@p15 { function = "uart0";  termination = "pull_up"; drive_strength_mA = < 8 >; }; // RX uart0
                  pin@p16 { function = "output"; termination = "pull_up"; polarity = "active_low"; }; // activity LED
                  pin@p21 { function = "output"; termination = "no_pulling";    }; // Camera shutdown
                  pin@p40 { function = "pwm";    termination = "no_pulling"; drive_strength_mA = < 16 >; }; // Left audio
                  pin@p45 { function = "pwm";    termination = "no_pulling"; drive_strength_mA = < 16 >; }; // Right audio
                  pin@p46 { function = "input";  termination = "no_pulling";    }; // Hotplug
                  pin@p47 { function = "input";  termination = "no_pulling";    }; // SD card detect
                  pin@p48 { function = "sdcard"; termination = "pull_up";    drive_strength_mA = < 8 >; }; // SD CLK
                  pin@p49 { function = "sdcard"; termination = "pull_up";    drive_strength_mA = < 8 >; }; // SD CMD
                  pin@p50 { function = "sdcard"; termination = "pull_up";    drive_strength_mA = < 8 >; }; // SD D0
                  pin@p51 { function = "sdcard"; termination = "pull_up";    drive_strength_mA = < 8 >; }; // SD D1
                  pin@p52 { function = "sdcard"; termination = "pull_up";    drive_strength_mA = < 8 >; }; // SD D2
                  pin@p53 { function = "sdcard"; termination = "pull_up";    drive_strength_mA = < 8 >; }; // SD D3

// I am using gpio pins 2, 3, 4, 9, 10, 11, 17, 22, 27 for my relays. Delete any you do not wish to use,
// or change the pin numbers to suit your purposes
// my relay board activates relays when a low signal is applied, so the pins are set with pull_up resistors, and active_high
// if your board activates the relays when a high signal is applied, you need to use pull_down resistors and active_low (I think)
                  pin@p2  { function = "output"; termination = "pull_up"; startup_state = "active"; polarity = "active_high"; }; // FIRST_RELAY
                  pin@p3  { function = "output"; termination = "pull_up"; startup_state = "active"; polarity = "active_high"; }; // SECOND_RELAY
                  pin@p4  { function = "output"; termination = "pull_up"; startup_state = "active"; polarity = "active_high"; }; // THIRD_RELAY
                  pin@p17  { function = "output"; termination = "pull_up"; startup_state = "active"; polarity = "active_high"; }; // FORTH_RELAY
                  pin@p27  { function = "output"; termination = "pull_up"; startup_state = "active"; polarity = "active_high"; }; // FIFTH_RELAY
                  pin@p22  { function = "output"; termination = "pull_up"; startup_state = "active"; polarity = "active_high"; }; // SIXTH_RELAY
                  pin@p10  { function = "output"; termination = "pull_up"; startup_state = "active"; polarity = "active_high"; }; // SEVENTH_RELAY
                  pin@p9  { function = "output"; termination = "pull_up"; startup_state = "active"; polarity = "active_high"; }; // EIGHTH_RELAY
// end of my settings

               }; // pin_config
               pin_defines {
                  pin_define@HDMI_CONTROL_ATTACHED {
                     type = "internal";
                     number = <46>;
                  };
                  pin_define@NUM_CAMERAS {
                     type = "internal";
                     number = <1>;
                  };
                  pin_define@CAMERA_0_I2C_PORT {
                     type = "internal";
                     number = <0>;
                  };
                  pin_define@CAMERA_0_SDA_PIN {
                     type = "internal";
                     number = <0>;
                  };
                  pin_define@CAMERA_0_SCL_PIN {
                     type = "internal";
                     number = <1>;
                  };
                  pin_define@CAMERA_0_SHUTDOWN {
                     type = "internal";
                     number = <21>;
                  };
                  pin_define@CAMERA_0_UNICAM_PORT {
                     type = "internal";
                     number = <1>;
                  };
                  pin_define@CAMERA_0_LED {
                     type = "internal";
                     number = <5>;
                  };
                  pin_define@FLASH_0_ENABLE {
                     type = "absent";
                  };
                  pin_define@FLASH_0_INDICATOR {
                     type = "absent";
                  };
                  pin_define@FLASH_1_ENABLE {
                     type = "absent";
                  };
                  pin_define@FLASH_1_INDICATOR {
                     type = "absent";
                  };
                  pin_define@POWER_LOW {
                     type = "absent";
                  };
                  pin_define@LEDS_DISK_ACTIVITY {
                     type = "internal";
                     number = <16>;
                  };
                  pin_define@LAN_RESET {
                     type = "internal";
                     number = <6>;
                  };
                }; // pin_defines
            }; // pins
   };
};

exit from the editor

Code: Select all

<ctrl> x
y
now we install the binary blob into /boot

Code: Select all

sudo dtc -I dts -O dtb -o /boot/dt-blob.bin dt-blob.dts
Now all you need to do is reboot your Pi, and all the relays should be turned off at boot time.

I've attached a zipped copy of the dt-blob.dts file to make things easier

Don't forget gpio pins 2 and 3 are gpio pins 0 and 1 on the version 1 board

I found the info to do this at the following places:
http://www.raspberrypi.org/documentatio ... uration.md
http://www.raspberrypi.org/documentatio ... dating.md#

Hopefully someone will find this useful, as it took me a day to get this working.
Attachments
dt-blob.dts.zip
dt-blob.dts file as shown above
(1.29 KiB) Downloaded 131 times

Return to “Automation, sensing and robotics”