direct connect Raspberry Pi GPIO to simple home made circuits
Pages: 1 2
RichardSP (8420) 12 posts |
As part of some fun circuitry activities using an Arduino Uno and a Raspberry Pi 3B+, I created two completely simple but quite separate circuits: |
Andrew Conroy (370) 740 posts |
If the Arduino Uno GPIO is 5V then you might need to adjust the value of the resistor since the Pi GPIO is 3.3V. Apart from that then you can connect LEDs (with suitable resistors) directly to GPIO in series or parallel, I’ve done this many times. A handy site to calculate the resistor value given the spec of your LED is here: http://www.hebeiltd.com.cn/?p=zz.led.resistor.calculator You can use any of the GPIO pins except GPIO 2 & 3 as they are allocated to IIC, and any of the ground pins. I posted example BASIC code to turn an LED on/off here: https://www.riscosopen.org/forum/forums/5/topics/651?page=6#posts-126371 Be aware that IIRC the maximum recommended current draw per GPIO pin is 16mA and 50mA total over all GPIO pins, so you should choose circuits and resistor values accordingly. |
RichardSP (8420) 12 posts |
Thanks for your assistance and advice. I discovered yesterday that I could download a copy of what looked like an old copy of “Raspberry Pi” for dummies and so I’ll also look at that and I’ll certainly look at your suggested hyperlinks. |
Andrew Conroy (370) 740 posts |
Can you reword that, I’m not sure I understand the question, sorry. Did you connect the Pi and the Arduino together?
I don’t know the spec of your LED, but if the resistor was fine on a 5V Arduino then it may well be fine for the 3.3V Pi. To be certain, check against the LED resistor calculator page I linked above. If you don’t know the spec of your LED (forward voltage, maximum current, etc.), then I’d recommend getting some that you do know the spec for! |
Dave Higton (1515) 3526 posts |
It isn’t possible to work out exactly what you’ve done, but here’s one piece of advice just in case: DO NOT connect two or more outputs together. Take that as absolute. |
Alan Adams (2486) 1149 posts |
And beware that on the 40-pin GPIO the 5 volt output pin is 180degrees from one of the ground pins. I discovered the problem with this when I plugged a ribbon cable in the wrong way round. On the other end of the ribbon cable was an interface that connected all the ground pins together. This resulted in 5 volta being applied back to several GPIO output pins. The RPi will no longet start up. |
RichardSP (8420) 12 posts |
I have two identical Raspberry Pi 3B+ computers; the original running on the version of Linux provided by the Pi. On the second Pi, I installed the version of RISC OS provided by Castle Tech and have, for the past few months, been happily writing BBC/Acorn programs that I wrote so many years ago. In order to familiarise myself with the use of pin connections on the Raspberry Pi, I consulted the Raspberry Pi home page for project ideas for connecting from the Linux based Pi to one or more LCD. and https://projects.raspberrypi.org/en/projects/leds-buzzers-scratch-games/2 Because the GPIO pins are so small, (I have a health-related issues with my hands), and acting on the advice of a retired academic (a professor of physics), I connected the Pi to my Arduino UNO and ran a sketch called Blink, then after that hooked up and LED, and using my modified sketch, I was able to get the LED flashing using the sketch language syntax. The cable that I used for that connection is shown under the heading “Required materials” at I did however use a 220 Ohm resistor. All good, no problems. So the way that I figure it, it should not worry the RISC based Pi if I connect the same set-up that I used to connect to the Linux based Pi. |
RichardSP (8420) 12 posts |
Forgot to add: I didn’t know about the specs for the coloured and the clear LEDs that I purchased so when it came to calculating the resistor value given in http://www.hebeiltd.com.cn/?p=zz.led.resistor.calculator |
Andrew Conroy (370) 740 posts |
Ah, sounds like you transferred data from the Pi to the Arduino by the Pi’s serial port GPIO pins then, in which case you are fine. The UART to USB converter board shown on that page does the voltage level translation between the 3.3V of the Pi and the 5V of the Arduino. If you connect then together without any converter, you’d blow up the Pi – see the big red warning at the top of that page! As a general rule, what OS the Pi is running won’t affect whether something will damage the GPIO, provided that the GPIO is appropriately configured. If you can safely light an LED from Linux on the Pi, then you can light the same LED from RISC OS on the Pi. A 220 Ohm resistor is likely to be fine, for an ‘average’ LED, it’s a value I’ve used with LEDs on the Pi before. It is always a good idea to find out the spec for the components you’re using, though. |
RichardSP (8420) 12 posts |
I think I remember where I messed up in the past. I was feeling a bit crook when I downloaded the Arduino software onto my Dell Windows-10 based DTP and I tested my little circuits on that – and not the Linux-based Pi 3 nor the RISC OS based Pi 3. All worked well. |
RichardSP (8420) 12 posts |
Can anyone have a squizz at the little BBC BASIC program that I knocked up: MODE 3 : *KEY1 CLS|MRUN|M : NrPins% = 40 : VDU 14 : REM Set auto-paging mode on : PRINT “This program checks the mode to which a selected pin is set” : PRINT ‘“If necessary, tap theI’m trying to wrap my head around the SYS and GPIO bunch of commands. Upon running the program I get: Any ideas: what is the significance of the values of the pin% = 4? |
David Pitt (3386) 1248 posts |
Firstly the demo BASIC code should be presented in a readable form, not all on one line and with smart quotes. <pre> REM MODE 3 *KEY1 CLS|MRUN|M NrPins% = 40 REM VDU 14 : REM Set auto-paging mode on : PRINT "This program checks the mode to which a selected pin is set" PRINT "If necessary, tap the key to view the status of all valid pins" pin% = 0 REPEAT pin% = pin% + 1 SYS "GPIO_ReadMode", pin% TO result% IF result% < 0 THEN PRINT "Pin "; pin%; " ó not a valid pin" ELSE PRINT "Pin "; pin%; " is set to "; result% UNTIL pin% = NrPins% REM VDU 15 : REM Set auto-paging mode off END I have never ever used the pins so cannot help much but this is taken, and shortened, from an RPi4 running the above in a TaskWindow. Pin 1 is set to 96 Pin 2 is set to 100 Pin 3 is set to 100 Pins 4 to 8 are set to 96 Pins 9 tp 27 are set to 32 Pins 28 to 40 are not valid pin3 I have found some info. It is a StrongHelp file. |
Julie Stamp (8365) 474 posts |
I think you mean result% = 4, but good question! It does take a bit of decoding, but they key is in this comment combined with pinout.xyz. Note in particular (if you’ve not worked it out already) that in So in your example, if pin% = 2 and result% = 4, then from Andrew’s comment we see that the mode is set to Alt 0. Looking up GPIO 2 (not pin 2!!!) on pinout.xyz, we see it’s “I2C1 SDA”, i.e. it’s configured to be one of a pair of wires for port I2C1. If your only interest is in GPIO, then you’ll want to set the mode to 0 or 1 (again see Andrew’s comment). There is also a very short guide here in the wiki. |
RichardSP (8420) 12 posts |
Thanks heaps Andrew, Dave, Alan, David and Julie. Looks like there’s a bit of homework to do (that is easier done while we are in covid lockdown). |
Andrew Conroy (370) 740 posts |
You could set up an array so that the array index was the pin number and the value was the GPIO number:
Then GPIO%(40) would give a value of 21, ie. pin 40 is GPIO21. I used a value of -1 to indicate this pin isn’t a GPIO pin. In use, this would just look like to write 1 to GPIO21.
Better programmers will probably suggest better ways of doing it. |
RichardSP (8420) 12 posts |
Thanks Andrew and David. I’ve never considered myself as a particularly good programmer so I really appreciate this input of yours. In addition, getting used to a forum is certainly a daunting experience. So question 1 how and what info source do will I find any reference to older method of directly accessing the GPIO registers? For instance in !Manual on my Pi 3B, the info is there bare-bones but it is difficult to know where to start. 2 Or is it on the ROOL forum somewhere? |
Andrew Conroy (370) 740 posts |
I wouldn’t worry too much about trying to access them directly, the GPIO module is there to make your life easy. Unless there’s a specific reason you need to (speed?) then stick to the simple code. |
RichardSP (8420) 12 posts |
Thanks heaps. For now I don’t require speed. For the time being, I’ll get more familiar with GPIO, pins, and correctly using the code and most of all, I don’t want to risk losing this RISC flavoured Raspberry Pi 3B. Having the experience of losing a Master 128 computer (not a robotics or control issue that was) and an Acorn RISC PC 5000 (same issue as for the Master 128) was enough! I will keep in touch! |
Alan Adams (2486) 1149 posts |
What the module won’t allow you to do is attach an interrupt service routine to a pin or pins. For the case I am doing fortunately I only needed to know what happened to 1 cSec accuracy, so I could hang a short polling routine off the TickerV vector, which runs at 100 times per second. |
Andrew Conroy (370) 740 posts |
Agreed, but unless I’ve missed something it doesn’t sound like Richard is trying to do anything more complex than lighting an LED or detecting a switch pressed at present. I think starting to suggest a beginner should be writing assembler routines or something to access GPIO might be a bit over the top! Keep it simple! |
RichardSP (8420) 12 posts |
Basically, I want to start connecting my RISC OS Pi to output devices (say one or more LED) and to learn to connect a switch and, down the track, other electronic bits. Most of all, I am very spooked that I might fry this Pi of mine. As I pick up skills, I want to learn the tougher stuff. Last Friday I put in an order for a publication from a mob (called “Drag N Drop”). That plus the ROOL forum plus my programming forays should augment the Fox Wimp PDF. |
Steve Fryatt (216) 2105 posts |
Or, do the sensible thing and don’t use assembly language. If BASIC can’t do what you want, there are plenty of other high level options: C, C++, Python, Lua, …
[citation needed]
Familiarity with For now, RISCOSitory has a good page listing known tutorials for different aspects of the platform – I’m sure that Vince can add more links if people think any have been missed. |
Dave Higton (1515) 3526 posts |
Entirely agree, for very sound reasons that have been discussed several times here. ARM keep altering the instruction sets, so assembly language is very laborious to port to a different CPU; whereas C is mostly a case of simply recompiling. Also your productivity will be higher in pretty much any other language than assembly. I’ve done lots of assembly language programming in my life (years ago), but I wouldn’t dream of doing it now. It’s indefensible IMHO. |
Paolo Fabio Zaino (28) 1882 posts |
I agree with Steve and Dave, avoid wasting time to learn AArch32 Assembly (it’s even an EOL on ARM Application CPUs). If you need performance, I would suggest C, it’s a mature language on RISC OS and if you need to learn how to do WIMP coding in C there is Steve’s excellent introduction, link on the RISCOSitory page. You can also try C++, but it’s not as mature as C, so I would prefer C over C++. If you want a BASIC like language, but that can also run fast compiled C libraries, then you want to try RiscLua, it’s solid and mature and can be extended using both Lua and compiled libraries written in multiple languages that supports ELF format. Python is getting also better and better on RISC OS, so if you have some knowledge of python you can try !Python as well, but I am not sure if it can already access the GPIO Module and the GPIO pins, while Lua can. So I would check that before you start playing with Python. The Drag’N’Drop book on WIMP programming in BBC BASIC is an excellent resource that you can combine with Martin Fox beginners guide, but IMHO the Drag’N’Drop guide is way better if nothing as for a fantastic reference manual and for extended controls especially for the type of applications you want to develop on controlling LEDs etc via GPIO, there are few chapters on sliders and rotary controls that are simply spot on. Also don’t forget that on the “Raspberry Pi Projects for Dummies” book there are instructions and projects to do exactly what you want to do using RISC OS: https://www.oreilly.com/library/view/raspberry-pi-projects/9781118766712/27_9781118766699-ch19.xhtml Welcome and good luck with your future projects on RISC OS :) |
Andrew Conroy (370) 740 posts |
Python certainly can access the GPIO module, I’ve replicated most of the standard rpi.gpio library, so I can use quite a bit of ‘off the shelf’ code for GPIO access without changing it for RISC OS. |
Pages: 1 2