MrCm
Posts: 2
Joined: Wed Dec 07, 2016 7:33 pm
Location: USA

HOWTO: Set GPIO pins to a known state on boot

Wed Dec 07, 2016 8:01 pm

I was having a lot of trouble getting one GPIO pin to stay high on boot and on rebooting. After a lot of digging I found a good solution. Hopefully this will help someone.

You can of course edit the config to make the pin low on boot, and set other options as well.

Create an overlay file: nano mygpio-overlay.dts You can change the name if you like, you'll just need to change the commands later to reflect this.

Add the following code to the file:

Code: Select all

/dts-v1/;
/plugin/;

/ {
   videocore {
      pins_3b2 { // Pi 3 Model B rev 1.2
         pin_config {
            pin@default {
               polarity = "active_high";
               termination = "pull_down";
               startup_state = "inactive";
               function = "input";
            }; // pin
            pin@p6 { function = "input";  termination = "no_pulling"; drive_strength_mA = < 8 >; };
            pin@p13 { function = "output";  termination = "pull_up";    drive_strength_mA = < 8 >; startup_state = "active"; };
         }; // pin_config
      }; // pins
   };
};
In my example above you can see I am setting GPIO pin 6 to an input with no pull up or down resistor. I'm also setting pin 13 to an output with a pull up resistor, and the pin starts high thanks to the startup_state = "active" part.

Use this page as a reference to edit the file as needed to fit your Raspberry Pi model and GPIO pin needs: https://www.raspberrypi.org/documentati ... uration.md

There is a newer blob example file here: https://github.com/raspberrypi/firmware ... t-blob.dts It appears to cover all revisions of the Raspberry Pi so far.

In my example I'm using a Raspberry Pi 3 B rev 1.2. If you were using a Raspberry Pi Zero you would change pins_3b2 in line 6 to pins_pi0

Once you are finished editing the file above, save it then compile it with

Code: Select all

dtc -@ -I dts -O dtb -o mygpio-overlay.dtb mygpio-overlay.dts
Next copy the resulting mygpio-overlay.dts file to your /boot/overlays folder with

Code: Select all

sudo cp mygpio-overlay.dtb /boot/overlays
Now add the following line to your /boot/config.txt

Code: Select all

device_tree_overlay=overlays/mygpio-overlay.dtb
Lastly reboot and check that everything works as expected.
Last edited by MrCm on Tue Jan 03, 2017 5:40 pm, edited 1 time in total.

thof
Posts: 18
Joined: Mon Mar 04, 2013 2:08 pm

Re: HOWTO: Set GPIO pins to a known state on boot

Tue Jan 03, 2017 5:17 pm

Cool! But unfortunately I'm not being able to get it working.
Should you also add a dtoverlay=mygpio to /boot/config.txt?

What I want to do is create an overlay to set gpio6 or gpio16 high on boot...

MrCm
Posts: 2
Joined: Wed Dec 07, 2016 7:33 pm
Location: USA

Re: HOWTO: Set GPIO pins to a known state on boot

Tue Jan 03, 2017 5:38 pm

thof wrote:Cool! But unfortunately I'm not being able to get it working.
Should you also add a dtoverlay=mygpio to /boot/config.txt?

What I want to do is create an overlay to set gpio6 or gpio16 high on boot...
Good catch! You need to add the following line to your /boot/config.txt

Code: Select all

device_tree_overlay=overlays/mygpio-overlay.dtb
I added this to my original post. Thanks!

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2944
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: HOWTO: Set GPIO pins to a known state on boot

Wed Jan 04, 2017 11:36 am

No, no, no. This is all wrong.

The OP is confusing the Pi firmware-specific dt-blob format, which is used for configuring pin functions during the early boot stages, and proper Device Tree overlays. This confusion is slightly understandable because the dt-blob is compiled using the Device Tree compiler - it uses DT syntax, but the semantics are completely different.

A custom dt-blob allows you to set pin functions, directions, drive strength and levels at boot time, but the only way to modify them is to create your own dt-blob.bin to override the default built into start.elf. This can cause problems if the contents of the default dt-blob changes, as can happen when new devices and features are added, since you are replacing everything rather than applying a small patch. If a custom dt-blob is what you want then the content of the "overlay" posted above is roughly what you would need to use, except that the patch would need to be applied to the current dt-blob.dts source file, then compiled into dt-blob.bin and placed in /boot.

The GPIO configuration mechanisms available to the kernel are not as flexible as I would like, but if all you want to do is set a GPIO high and keep it high permanently then a gpio-hog will do the job. If you want the pin initially set high but to be left free for something else to take it over later then that is more tricky.

Can you explain your use case more thoroughly?

thof
Posts: 18
Joined: Mon Mar 04, 2013 2:08 pm

Re: HOWTO: Set GPIO pins to a known state on boot

Wed Jan 04, 2017 12:05 pm

Somehow I already had a feeling some things are being confused here, thanks for the explanation!

For me the ideal situation would be to set a pin high (power an LED) as soon as power is applied and be later able to change the state of that pin in my program. This because my RPi is built in and I would like to show a status light to the user, indicating power is applied and the device is booting.

Changing dt-blob could also work, I just have to keep in mind that with an update things might break and I have to apply the patch again.

update: applying above changes to dt-blob.dts and compiling it to dt-blob.bin and placing in /boot works! It is just not as elegant as an overlay which acts more as a plugin and is more future proof.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2944
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: HOWTO: Set GPIO pins to a known state on boot

Wed Jan 04, 2017 4:21 pm

If you can stand a bit of a delay before the LED comes on (15-20s), the quick and dirty solution is to call raspi-gpio from /etc/rc.local.

Edit /etc/rc.local (you'll need root permission, so use sudo) and add this before the "exit 0" at the end:

Code: Select all

raspi-gpio set 6 op dh
Now when you boot your LED will come on automatically.

You may also see that the LED is on weakly before it comes on fully - this is because GPIO 6 defaults to being an input with a weak pull-up. If you can add an extra circuit to use GPIO 6 as just a switch rather than the current source (essentially you are adding an amplifier), you don't even need the raspi-gpio line - the LED will come on immediately, but will be switched off if you change the pin to an output driving low (raspi-gpio set 6 op dl).

Return to “Advanced users”