Posts: 30
Joined: Thu Jul 16, 2015 9:02 pm

gpio value in global variable

Fri Feb 05, 2016 10:00 pm

Is it possible to reference the value of the pin 7 outside this function in a global variable?

gpio.setup(7, gpio.DIR_IN, readInput);

function readInput() {
gpio.read(7, function(err, value) {
console.log('The value is ' + value);


Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: gpio value in global variable

Mon Feb 08, 2016 11:29 am

Depends on 'how global' do you need. Maybe this is enough:

Code: Select all

var gpio7value = false;

gpio.setup(7, gpio.DIR_IN, readInput);
function readInput() {
    gpio.read(7, function(err, value) {
        gpio7value = value;
        console.log('The value is ' + gpio7value);

http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Posts: 7
Joined: Sat Apr 08, 2017 11:39 pm

Re: gpio value in global variable

Mon Jun 18, 2018 4:12 pm

Hi, I'm battling with the same issue. I've used your response and added console posts inside and outside the function:
2 const http = require('http');
3 const express = require('express');
4 const MessagingResponse = require('twilio').twiml.MessagingRe sponse;
5 const bodyParser = require('body-parser');
6 const app = express();
9 var gpio = require('rpi-gpio');
10 var gpio13value = false;
12 gpio.setup(13, gpio.DIR_IN, readInput);
14 function readInput() {
15 gpio.read(13, function(err, value) {
16 gpio13value = value;
17 console.log('iniside The value is ' + gpio13value);
18 return(gpio13value);
19 });
20 console.log('outside The value is ' + gpio13value);
21 }

The results are:
[email protected]:~/Desktop/test/SMSPI $ node test1.js
outside The value is false
iniside The value is true

Can you help?

Posts: 9325
Joined: Tue Jul 17, 2012 3:02 pm

Re: gpio value in global variable

Thu Jun 21, 2018 7:26 am


Did anyone notice that this is Javascript code being presented not Java? A totally different language. This question should be in the "other programming languages" section, or preferably in a Javascript section if we ever get one. Grumble, grumble...

Before doing anything else add a check for errors in your readInput callback, as shown in the rpi-gpio documentation:

Code: Select all

function readInput(err) {
    if (err) throw err;
Why do you have "return(gpio13value);" at the end of that function? The documentation does not call for it. It goes nowhere.

I think you will find that your program is working correctly.The problem is that you do not yet understand Javascript. Hint, it's not Java.

When your program runs it first calls gpio.setup(), passing it a function, readInput, that is to be called some time later when the setup is completed.

Next it calls console.log() which prints the value of gpio13value. Which is false because that is what you set it to at the top of the program.

Some time later, when setup() is completed it will call that callback function, readInput. That in turn will read the input by calling gpio.read(). Again a call back function is passed to gpio.read(). This time as an anonymous function parameter.

Some time later still, when the GPIO has been read that anonymous callback function will call console.log(), this time printing the new value of true (Assuming the input is set).

The main point of all this is that those callback function that you are creating and passing to gpio are not executed immediately, they are executed some time later when the work is done. The is the asynchronous nature of the event based programming model of Javascript. Learn it.

I'm pretty sure that if you change that console.log() at the end of your program to something like this:

Code: Select all

setInterval(function() {
    console.log('outside The value is ' + gpio13value);
}, 1000};
you will see gpio13value change.

Also, please use code tags when presenting examples here so that the formatting is preserved nicely and we don't have to struggle to read it.

Posts: 9325
Joined: Tue Jul 17, 2012 3:02 pm

Re: gpio value in global variable

Thu Jun 21, 2018 7:37 am

In general I would say that if one feels the need for global variables there is something wrong with ones program design. The approach is probably wrong. Sounds like you are trying to use Javascript as if it were C or Java.

For example in this case, by making gpio13value global you ended up using its value before it had been set by gpio.read(). Clearly not what you actually wanted to do. You created what is known as a "race condition".

What actually is it that you are trying to do?

Return to “Java”

Who is online

Users browsing this forum: No registered users and 2 guests