Articles on accessing RPi pins

For issues with communication ports, protocols, etc.
Post Reply
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Articles on accessing RPi pins

Post by BasicCoder2 »

There doesn't seem to be any interest amongst the experts on using FreeBASIC for GPIO access on the Raspberry Pi so it is going to be an uphill battle to get anything working particularly as I have to learn all the Linux stuff at the same time. On the off chance anyone else is interested I found the following that might help.

An article about how to access the RPi gpio pins with Raspbian that uses the Linux file system.
http://codefoster.com/pi-basicgpio/

And another,
https://sites.google.com/site/semillero ... rials/gpio

Here are examples using other languages including BASIC languages like RTB,bwBASIC,BBC Basic but no FreeBASIC examples.
https://elinux.org/RPi_GPIO_Code_Samples
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: Articles on accessing RPi pins

Post by speedfixer »

Define: expert.
And, from traditional educators and computer scientists - if you have even learned BASIC at any time in your life, you can't be an expert because you are 'broken.' That would be a joke if it wasn't so widely believed to be true.

Now, seriously:

It doesn't look like you can address the pins DIRECTLY from FB. I doubt you want the work to embed asm for that. The modern OS (correctly) keeps that from being simple. However, there are many fine libraries to use.

How fast (how little the delay) do you need in accessing the pins?

Fastest seems to be pigpio daemon.
Next would be any of many libraries - pigpio, wiringPI, BCM2835. I believe Joshy has a version.
Then a language: Lua, Python, others. Most use one of the above libs.
Then direct file access to the system mapped files.

Each has its own advantage.

I chose the pigpio daemon. I ported the headers myself. Works great. (7 files 75k tailored to my build systems and way more complex than required by anyone else)

I used fbfrog as the starting point and took a couple of hours to get something working.
You shouldn't have too much trouble.

I use the PI to talk to my arduinos in my security systems.

david

( As of 7/2019: For anyone interested: stay away from the PI4. Software just not ready. AND -- While trying to get the next Linux kernels ready for PI4, the Buster versions are in trouble for all the other PIs. Stay away from Buster. I'm certain all the trouble will eventually be fixed, but they are still breaking things to fix other broken things. Search the Pi forums.)
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Articles on accessing RPi pins

Post by BasicCoder2 »

speedfixer wrote:Define: expert.

Clearly there are posters here that are very knowledgeable compared with me when it comes to programming and hardware and I suspect could without much effort do the things I would like to do but have no interest in doing so.

It doesn't look like you can address the pins DIRECTLY from FB. I doubt you want the work to embed asm for that. The modern OS (correctly) keeps that from being simple. However, there are many fine libraries to use.

Not interested in libraries at this stage. As codefoster wrote:
"I think people that love technology love understanding how things work. When I was a kid I took apart the family phone because I was compelled to see what was inside that made it tick. My brother didn’t care. If it made phone calls, he was fine with it. I had to understand."
http://codefoster.com/pi-basicgpio/

That is what I am like.

Although you can't access the pins directly it appears on Linux all hardware is accessible via the file system.

It is probably possible to use a c library with FreeBASIC which I think Python uses?

I did make the mistake of using the 5v pin not realising the pins for i/o are only 3.3v so I hope I haven't done damage to my RPi. I will fire up the old Python and use a tutorial on turning on LEDs and reading button switches and if that works at least I know the RPi is ok.

I use the PI to talk to my arduinos in my security systems.

But do you use FreeBasic programs on the RPi to talk to the Arduino ?

Perhaps I will just stick with Python for a while to get an idea how things work and try FreeBasic again later.
https://learn.sparkfun.com/tutorials/raspberry-gpio/all
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Articles on accessing RPi pins

Post by D.J.Peters »

You can use the pins via FreeBASIC file read and write

if something goes wrong try sudo at first

sudo ./yourprog with GPIO access

if sudo works than you have to add your "normal" user account to the group with the rights to access the GPIO pins !

sudo adduser pi gpio

Joshy
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Articles on accessing RPi pins

Post by BasicCoder2 »

The Python version worked as expected so the RPi has not been damaged by my earlier mistakes!!
Holding the button switch down turns off one LED and turns on the other as a flashing LED.
Image

My aim is to write a FreeBASIC translation of the Python program or its C version (shown below).

I don't know if there is some way to include the <wiringPi.h> in a FreeBASIC version?

https://learn.sparkfun.com/tutorials/raspberry-gpio/all

Code: Select all

#include <stdio.h>    // Used for printf() statements
#include <wiringPi.h> // Include WiringPi library!

// Pin number declarations. We're using the Broadcom chip pin numbers.
const int pwmPin = 18; // PWM LED - Broadcom pin 18, P1 pin 12
const int ledPin = 23; // Regular LED - Broadcom pin 23, P1 pin 16
const int butPin = 17; // Active-low button - Broadcom pin 17, P1 pin 11

const int pwmValue = 75; // Use this to set an LED brightness

int main(void)
{
    // Setup stuff:
    wiringPiSetupGpio(); // Initialize wiringPi -- using Broadcom pin numbers

    pinMode(pwmPin, PWM_OUTPUT); // Set PWM LED as PWM output
    pinMode(ledPin, OUTPUT);     // Set regular LED as output
    pinMode(butPin, INPUT);      // Set button as INPUT
    pullUpDnControl(butPin, PUD_UP); // Enable pull-up resistor on button

    printf("Blinker is running! Press CTRL+C to quit.\n");

    // Loop (while(1)):
    while(1)
    {
        if (digitalRead(butPin)) // Button is released if this returns 1
        {
            pwmWrite(pwmPin, pwmValue); // PWM LED at bright setting
            digitalWrite(ledPin, LOW);     // Regular LED off
        }
        else // If digitalRead returns 0, button is pressed
        {
            pwmWrite(pwmPin, 1024 - pwmValue); // PWM LED at dim setting
            // Do some blinking on the ledPin:
            digitalWrite(ledPin, HIGH); // Turn LED ON
            delay(75); // Wait 75ms
            digitalWrite(ledPin, LOW); // Turn LED OFF
            delay(75); // Wait 75ms again
        }
    }

    return 0;
}
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: Articles on accessing RPi pins

Post by speedfixer »

Yes, I use the PIs to talk to the arduinos. Simply opening a a port from the PI to the arduino let's you talk. You just have to understand that YOU decide what and when EACH will say to each other. You basically invent your own protocol to have one control the other.

For simple pin control, using the system files method is ok.
If you try to do any fine control of timing, you will see that more is going on in the PI system that will negate your ability to achieve precision. A PI doesn't have the horsepower to do too much at one time and have them look like each is a task alone. That's where the external libs come into play. When you want a more complex task, like controlling multiple PWM devices independently, you need a more consistent communication stream.

I use my desktop (PC) to talk to a headless PI that has a program that monitors an arduino.

For example, one arduino has 3 humidity/temp sensors, 5 temp sensors, one 16 channel PWM multiplexor. The PWM chip has 4 sets of fans, 8 Peltier devices, some LEDS. This controls the temperature of my network device cabinet in my (hot) garage, and also gives me inside and outside temp and humidity info. Nothing has to happen too fast, so control by the arduino is fine.

The PI oversees the arduino. It has the smarts. The control program in the PI monitors the box temps and tells the arduino how nuch power to use to control the Peltiers. And various fan speeds. If the humidity in the box is too high, a dehumidify cycle is started that will pull water out of the box. If temps are out of bounds the PI will send a notice into my network. My house monitor PC (running Home Assistant software) monitors the system. When something is out-of-bounds, it will notify me by cell phone - wifi or text. I can also demand check temps and more with my cell phone with that package.

Another arduino monitors the camera network system. Another arduino/PI pair controls the door locks. One PI is the responder to the tracker system. Cell phones in the network and facial recognition tells me who is where.

Well, that's the vision, anyway. The network box with Peltiers/sensors/fans/arduino is almost complete. The communications libraries for PI/arduino/PC are well established and work satisfactorily. A good part of the tracking system is in place: I know who is where and when an identified visitor arrives. The back doors are in place and work fine. This means someone can approach the door and it greets the visitor and unlocks the door for them automatically. And (depending on the guest) will notify me or my son of who has arrived. This works now.

+2 A/C systems, 2 food freezers,etc. My solar system is large enough that I still get a credit from the electric company. I see myself as a fortunate person that has done a bit of hard work and thinking ahead of the curve, and not made too many expensive mistakes. (Well, one divorce that cost me 6 houses. Was cheap to get out of that pain. I cried for 5 minutes. Happily married now; proud of both my sons. Life is good. I am lucky.)

What are your goals? Do you have an end application in mind, or just want to have fun with beeps, squeeks, and blinking lights. I get sidetracked doing that stuff, all the time. (Just like I get sidetracked talking too much.)

I used the wiringPI lib, but prefer the pigpiod libs. Study them. The wiringPI lib SEEMS to be preferred by most, but I think that is because the Python interface is so simple and used by both Sparkfun and Adafruit. The pigpio libs also have a Python interface.
I have my own 'C' interface FB headers for the pigpiod daemon. Once the daemon is started in the system (can be automatic on boot) no sudo is required to run your app (compiled from FB, for example) and the performance is closer to real time.

Play, take your time to get comfortable talking to the PI. I will work up headers to each this week. Then fxm, Joshy and others can fix my mistakes, and everyone can play. (I think there should be an 'Other' category - not Win, Dos, Linux. Raspian is Linux, but using it on the PI is as different as using it on WIn from Linux.)

david

(yeah, everyone: I know I talk too much. Too many years teaching the tech crap at work. Blank faces: never know if the students/audience got the message. Knew I was doing something right because my classes were always overbooked. Good for teaching, but bad reinforcement for real life.)
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Articles on accessing RPi pins

Post by D.J.Peters »

Hardware access on ARM devices are configured via an device tree compiler (for the kernel)

The device tree maps the GP IO's pin's in the /dev/XYZ nodes
(there are more under the hood)

/sys/class/gpio

So you can use pseudo code:

echo "1" > "dev/featueXYZ/abc
or
cat "dev/featueXYZ/abc

to set a pin config or read a pin config
to set a pin value (0/1) or read a pin state

With other words ALL programing language with file I/O can use the pin's !

Joshy

you can replace echo with print in FreeBASIC

Code: Select all

 make pin 17 accessable 
> echo "17" > /sys/class/gpio/export

ERROR: export: Permission denied

you need root rights!

do this:
> su root

or you can change the permissions with chmodd

> sudo chmod 222 /sys/class/gpio/export /sys/class/gpio/unexport

writing a 17 in /sys/class/gpio/export created a new folder : /sys/class/gpio/gpio17/

set pin direction as output
> echo "out" > /sys/class/gpio/gpio17/direction

set pin Value to 1
>echo "1" > /sys/class/gpio/gpio17/value

clean up
> echo "17" > /sys/class/gpio/unexport
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Articles on accessing RPi pins

Post by BasicCoder2 »

speedfixer wrote: What are your goals? Do you have an end application in mind, or just want to have fun with beeps, squeaks, and blinking lights.)
Perhaps a bit more than that :)

My current project, when I get time, is thinking about and tinkering with an old dream to build an autonomous domestic robot. Probably will never finish it but I hope maybe to get it mapping and navigating a house. Perhaps have a click on vacuum cleaner as an excuse for spending the money on it!

At this stage it can do dead reckoning using the motor encoders, much like turtle graphics and recently, using FreeBASIC on a laptop, I can control it by sending it commands via a wireless keyboard. Maybe I can replace the laptop with a Raspberry Pi. Also maybe use the Rpi gpio pins instead of the usb port to communicate with the Arduino.

Image
Last edited by BasicCoder2 on Aug 05, 2019 21:55, edited 3 times in total.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Articles on accessing RPi pins

Post by BasicCoder2 »

@Joshy,
Thank you for your responses.
I have printed them out for reference for when I get back to work on the projects.
It looks as if MrSwiss has some interesting code that I will try out later today.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

pc to Rpi interfacing

Post by BasicCoder2 »

speedfixer wrote:I use my desktop (PC) to talk to a headless PI that has a program that monitors an arduino.
Would you be interested in sharing any of that? Does the PC use FreeBASIC code?
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: Articles on accessing RPi pins

Post by speedfixer »

I will share.
I need to trim my terminal program a bit: it is too specialized - and large and complex - to be useful to any one else right now. It is a monitor and process manager first, then a terminal program. It does use another thread for receiving. Study coderjeff's examples to get the idea.

My code is the same for PC and RPi. I write/prove it on the PC, then finalize it on a Pi. I use several tricks to make my life easier, but it is the exact same code. (I have one common server for all my source code.)

Pin access can be tricky. For both the Pi and Arduino.
Simple pin commands - on/off - for LEDs are trivial, but give you two things: appreciation that you can't make mistakes when writing 'programs' and a sense of success that you actually did something. When it gets more complex, you usually need more.

With one Arduino, I take 8 temperature and 3 humidity readings, control 6 fans, and control the current to 8 Peltier devices. I have one PCA9685 on it. And I have other Arduinos for other jobs. My code for the Arduino wouldn't help you, much, other than how I talk to it. But even that is more complex than anyone would ever want to just browse through. My high-level libraries manage talking between the Arduino and my FB term/manager. Next layer manages the devices on the Arduino. There are four 'C' device libraries below my manager that had to be adjusted to be 3rd tier responders to my control program.

I tried all this control from a Pi, at first. Easy to talk to, more horsepower/memory, higher level languages, etc. BUT - any OS on the Pi eats up so much realtime that fine control of more than one complex task just wasn't going to work. I got everything working on the Arduino in a week after I had killed a month just getting basics to work on the Pi. So, the Pi has high level management tasks (how fast the the fans run, how cold the Peltiers should be, when to run the de-humidify cycle, send notices/warnings about box/garage/outside temp/humidity, etc.) and the Arduino does the shoveling (current/speed control, sensor reads, etc.) Each does its job well.

But, yes, I only use FB for communication TO the Arduino.

I couldn't do much on the Arduino without the very fine libraries that others have written. Even (especially) on the Pi, other libraries are MUCH faster for complex operations. Better to invest to learn how to use the work of others than re-invent the wheel.

I don't think your interest is unique. And I expect that St-W's arm builds are used more than he thinks.

MODERATORS: I suggest a 'PI, BB, and other arm' topic would be helpful.

Any topic opened has to be decided to be opened in Linux, or general, or other places. I think that when Pi or another hardware or OS that is less-mainstream is mentioned, many just avoid. Terminology is different. Still, good food for the hungry, but avoided like the plague by many.

For a newbie, there are subtle differences, especially in terminology, that make things harder than needed.
For me and many, Pi and Linux aren't difficult to get up and running easily with FB. I think we should paint that kind of picture for visitors.

david

(again, i talk too much.)
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Articles on accessing RPi pins

Post by coderJeff »

speedfixer wrote:MODERATORS: I suggest a 'PI, BB, and other arm' topic would be helpful.
Could this be part of the "Hardware Interfaces / Communication" sub forum, somehow?

Possibly altering the name or description of the sub forum?
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: Articles on accessing RPi pins

Post by speedfixer »

That might be a better place.

Perhaps:

Interface to remote systems (Pi, BB, etc.)

Long, but descriptive. The intro sticky would make it clear that the intent is to talk to other responsive, programmable systems that have an OS.
Also, any code or libraries destined for that other system would be limited ONLY to the communication setup and interface, not the support of any features on that other system.

For example, many libraries are supported and introduced in the Libraries forum. They should relate to anything that FB might be able to talk directly to.
Here, the topics would be generalized to system-to-system or non-PC platforms.

Any question/problem using simply native OS routines through FB should be in General. General FB syntax and usage.
For example, any system talking to a serial port could be discussed under the library entry used or in General.
If the problem appears to be specific to a particular non-PC platform, then this new sub forum would be appropriate.
A few threads would probably need to stickied for a few very hot topics for quick access by newbies. More specific install instructions and common problems per platform, for example.

On the Pi, there are many low-level interfaces and features targeted more frequently by 'amateurs' (hobbyists? beginners? not really the correct term, but ...)
FB can get to many of those services and features. This would be an appropriate place to discuss direct access, or library interfaces to those features, through FB, of course.

A sticky for Arduino and FB, for example. Any host system.

Many people use other systems, like an Arduino, that FB can communicate with. Help with interfacing between an FB front-end and its host for talking to those other systems would be appropriate here.

Most extreme would be small code snippets, or even a tiny sketch, for the Arduino to clarify how FB could/should interface with it. After that, those systems all have their own support forums. For example, it seems the Arduino community is as eager to help newbies and be nice about it as we are here.

Moderator might have to remind someone that the forums are a language support system, not hardware. (A hardware support library discussion would be in Libraries, for a specific library. IF that library developer wishes to help.)

david
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: Articles on accessing RPi pins

Post by speedfixer »

I reconsider.

Hardware is hardware.
The Pi and others are full systems. Other platforms.

Usually, we expect a library to solve a specific problem.
To another software system.
To hardware.
To provide new/different functionality.

Using FB on another platform can involve a different kind of assistance - lot more than just: use this lib, install that software in this way.
Certainly, FB began as a (not) QB clone - implying MS pc systems, but it obviously has become much more than that.

I have no clue how many DOS user's are still out there, and I certainly see the argument for continued support. Simple: $$$ Cost of eq in less affluent communities is a good enough argument for me. Many that are decades away from DOS still help. I applaud that. But very painful for a developer ecosystem. (Please: I need more chains on my back!)

But if we still support DOS, there are many more potential users coming along that wonder what the support for the Pi, BB, Android(!?!) should be. And for those that like to spend $$$, even Apple.

Just because a Pi can be held in your hand like a toy, many, many people see and use it as far more than a toy.

On a totally unrelated side note:
What is going to happen when X11 and OGL die in favor of Wayland, Vulcan, etc.?

And, see: https://www.phoronix.com/scan.php?page= ... px=MTA5MzA

david
Post Reply