bfinio
Posts: 38
Joined: Mon Oct 14, 2013 8:53 pm

Scratch 2 GPIO pull-up & pull-down resistors

Mon Jul 22, 2019 8:16 pm

Hi - I've tried searching for this topic on Google and the forums with no luck. For Scratch 1.4 the documentation includes how to set internal pull-up/pull-down resistors using the "broadcast" block:

https://www.raspberrypi.org/documentati ... /README.md

I couldn't find any such information for Scratch 2:

https://www.raspberrypi.org/documentati ... /README.md

Questions:
  • Is it still possible to set pull-up/pull-down manually in Scratch 2?
  • What is/are the defaults? From some quick testing it appears that I have some pins that default to pull-up and others that default to pull-down.
  • Have the defaults changed between different versions of the Pi? I originally wrote something (using the defaults) on a 1B+ several years ago, now I'm trying the same code ported to Scratch 2 on a 3B+ and I'm getting different behavior on one of the pins (I'm using GPIO 4, 7, 8, and 10 - on the 1B+ I think they all defaulted to pull-up, on the 3B+ it seems like GPIO10 defaults to down).
Any help appreciated.

User avatar
joan
Posts: 14369
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Scratch 2 GPIO pull-up & pull-down resistors

Mon Jul 22, 2019 8:27 pm

I don't know about Scratch.

The following applies to all Pis.

GPIO 0-8 default to internal 50k pull-ups at power-up.

GPIO 9-27 default to internal 50k pull-downs at power-up.

Page 102 of http://www.raspberrypi.org/documentatio ... herals.pdf

In addition GPIO 2/3 have external hard wired 1k8 pull-ups (not sure if this applies to the compute modules).

bfinio
Posts: 38
Joined: Mon Oct 14, 2013 8:53 pm

Re: Scratch 2 GPIO pull-up & pull-down resistors

Mon Aug 05, 2019 2:57 pm

Thanks - I think that resolves my confusion; previously we were using a third-party solution (http://simplesi.net/) and I think that initialized all pins to pull-up. Unfortunately I still haven't had any luck figuring out whether you can toggle this manually in Scratch 2.

spl23
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 378
Joined: Fri Dec 26, 2014 11:02 am

Re: Scratch 2 GPIO pull-up & pull-down resistors

Tue Aug 06, 2019 10:13 am

bfinio wrote:
Mon Aug 05, 2019 2:57 pm
Unfortunately I still haven't had any luck figuring out whether you can toggle this manually in Scratch 2.
No, you can't - the interface via which the GPIO pins are accessed in Scratch 2 does not allow pull-ups or pull-downs to be configured, so you'll need to use their default states, or use some other mechanism external to Scratch to set the pulls.

hippy
Posts: 6089
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Scratch 2 GPIO pull-up & pull-down resistors

Tue Aug 06, 2019 4:06 pm

Is the source code for the Scratch 2 GPIO extension available ? It should be a pretty easy job for someone who knows what they are doing to add an extra block which allows for pull-up setting.

Found it : sudo nano /usr/lib/scratch2/scratch_extensions/piGPIOExtension.js
scratch2.jpg
scratch2.jpg (61.48 KiB) Viewed 3245 times
Adding the block was easy enough but it seems the extension uses sysfs for the GPIO interaction which I am not familiar with. Not sure if that's even possible. I am guessing that's what spl23 meant by the interface not supporting it.

It might be possible to use the 'remote' extension, or change the sysfs calls to use something else.

Basically; you need something else, an extension which can do what you want.

spl23
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 378
Joined: Fri Dec 26, 2014 11:02 am

Re: Scratch 2 GPIO pull-up & pull-down resistors

Tue Aug 06, 2019 5:12 pm

hippy wrote:
Tue Aug 06, 2019 4:06 pm
[Adding the block was easy enough but it seems the extension uses sysfs for the GPIO interaction which I am not familiar with. Not sure if that's even possible. I am guessing that's what spl23 meant by the interface not supporting it.
Yes, that's the bit that isn't possible. The sysfs interface has no commands which allow you to set pull-ups. Adding a new block is trivial, but there is no way to change pull-ups via that interface. You could conceivably write a block which accessed another interface, like raspi-gpio, but you'd need a whole different syntax and to change a bunch of permissions within Electron to allow access to system commands - it's an awful lot of fiddling about to get it to work.

It's probably not impossible, but it's really not a trivial change, and given that Scratch 2 has worked like this for a couple of years now and the release of Scratch 3 will happen in the not-too-distant future, it's not a change we are likely to make.

bfinio
Posts: 38
Joined: Mon Oct 14, 2013 8:53 pm

Re: Scratch 2 GPIO pull-up & pull-down resistors

Wed Aug 07, 2019 11:51 am

Thanks spl23! The original motivation here is that I have some old circuit diagrams I wanted to re-use for some projects that we're updating to Scratch 2 (I work for a small nonprofit in the US with our own custom RPi kit). I was trying to avoid changing the diagrams if possible. At this point it sounds like will be much easier for me to just switch to different pins that have the default settings I want, or use an external pull-up resistor.

Any idea if this functionality will be available in Scratch 3, or any chance you can elaborate on "not-too-distant future"? I've looked back through the blog but haven't seen anything about Scratch 3 since this post back in January: https://www.raspberrypi.org/blog/scratc ... -upgrades/

Thanks again!

hippy
Posts: 6089
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Scratch 2 GPIO pull-up & pull-down resistors

Wed Aug 07, 2019 3:32 pm

spl23 wrote:
Tue Aug 06, 2019 5:12 pm
It's probably not impossible, but it's really not a trivial change, and given that Scratch 2 has worked like this for a couple of years now and the release of Scratch 3 will happen in the not-too-distant future, it's not a change we are likely to make.
I got it working and without too much effort, worked just by changing the JavaScript. But I only went so far as getting it to execute an external program as proof of concept. One would have to tweak the spawn() and its parameters to use "gpio" or something else -

Code: Select all

const {spawn} = require('child_process')

Code: Select all

ext.set_pull = function (pin, val) 
{
  if (pin === '' || pin < 0 || pin > 27) return;
  var result = spawn('xxx', [ pin, val ]);
};

Code: Select all

[' ', 'set gpio %m.gpios pull to %m.pulls', 'set_pull', '', 'no pull' ],

Code: Select all

pulls: [ 'no pull', 'pull-up', 'pull-down' ],
As you say; it becomes redundant when Scratch 3 off-line arrives, so this is about as far as I'm going to go with it.

spl23
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 378
Joined: Fri Dec 26, 2014 11:02 am

Re: Scratch 2 GPIO pull-up & pull-down resistors

Wed Aug 07, 2019 5:03 pm

bfinio wrote:
Wed Aug 07, 2019 11:51 am
Any idea if this functionality will be available in Scratch 3, or any chance you can elaborate on "not-too-distant future"?
Yes, this functionality will definitely be available in Scratch 3 offline.

As for "not-too-distant future", I couldn't possibly say. But, on a completely unrelated point, had you seen that this event - https://scratchconferenceeurope.raspberrypi.org - is happening in a couple of weeks...? ;)

spl23
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 378
Joined: Fri Dec 26, 2014 11:02 am

Re: Scratch 2 GPIO pull-up & pull-down resistors

Wed Aug 07, 2019 5:13 pm

hippy wrote:
Wed Aug 07, 2019 3:32 pm
I got it working and without too much effort, worked just by changing the JavaScript.
I'm surprised about that - Electron usually requires you to explicitly set permissions to use things like spawn (basically anything which wouldn't be allowed in Chromium), so it's a little strange that it didn't need that. But well done!

As mentioned above, the ability to set pulls is included in the GPIO blocks in the offline version of Scratch 3.

Forris
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 279
Joined: Fri Jan 06, 2012 7:46 pm

Re: Scratch 2 GPIO pull-up & pull-down resistors

Thu Aug 08, 2019 11:59 am

bfinio wrote:
Mon Aug 05, 2019 2:57 pm
Thanks - I think that resolves my confusion; previously we were using a third-party solution (http://simplesi.net/) and I think that initialized all pins to pull-up. Unfortunately I still haven't had any luck figuring out whether you can toggle this manually in Scratch 2.
That 3rd party solution gives you the same functionality for Scratch 2 too:

http://simplesi.net/scratch2gpio/

Return to “Scratch”