Instable port reading



The GrovePi + that I received last week has some problems when reading from a digital port.
It often reads value 255 while it should just read 0.
The opposite is also true, it reads a 0 instead of 255.

I solved the problem by writing a script that checks n consecutive reads that have the same value.

I have the problem with a simple Grove button or a self made switch connected via a screw terminal an a pull down resistor of 10K.

Anyone else with the same problem and/or alternative solution?

Kind regards,


Hey Freddy,
Can you post the program that you are using and the output that you get.



Hi Kara,

Many thanks for your reply.
I re posted my request yesterday because I was not sure that it was send.
It did not showed up in my list.

The problems I have is that the reading of the digital ports.
They are not stable. This means that I have from time to time a reading that the button was down while nothing was touched.
The opposite is also true. When the button is pushed, the reading is randomly wrong.

I included the program but unfortunately some comments are in Dutch (I am from Belgium).

What you should know is that the function ‘LeesSwitch(button,n)’ is in there in order to solve that problem.
I read the switch status for n times and if they are all the same, i accept the value.

I hope you could give me some advise.
By the way, is there a way to force an key interrupt for the Grove digital ports like 'GPIO.wait_for_edge(23, GPIO.FALLING)'for the normal ports?

Kind regards,


Hi Karan

I receive sending errors, so here is the listing (still under development):

Copyright 2015 Freddy <>

Note the dht_sensor_type below may need to be changed depending on which DHT sensor you have:

0 - DHT11 - blue one

1 - DHT22 - white one, aka DHT Pro or AM2302 - comes with the GrovePi+ Starter Kit

2 - DHT21 - black one, aka AM2301

from grovepi import *
from grove_rgb_lcd import *
import datetime
import grovepi

relay = 2
switch = 3
led = 4
water = 5
dht_sensor_port = 7 # DHt sensor - port 7
dht_sensor_type = 0


Definier de switch toestanden

import itertools
mySW = itertools.cycle([0,1]).next

LED aanzetten


#Startminuut bepalen
beg_min =’%M’) #Minuten van de klok
nu_min = beg_min

Om erzeker van te zijn dat er bij opstart onmiddellijk een melding is

Datum en tijd

nu =’%d-%m-%y %H:%M’)

Declaratie van menu_val

menu_val = 0
beg_mail_val = 0
#NuVal = beg_mail_val

[temp,hum ] = dht(dht_sensor_port,dht_sensor_type) #Get the temperature and Humidity from the DHT sensor
t = str(temp)
h = str(hum)

Mess = “T:” + t + “C H:” + h + "%"
Mess = Mess + “\n” + nu

Meetfunctie bepalen

def Meet(waarde):
#print "Meting minuut: " +’%M’) + " minuten"
global Mess
#Datum en tijd
nu =’%d-%m-%y %H:%M’)

[temp,hum ] = dht(dht_sensor_port,dht_sensor_type)		#Get the temperature and Humidity from the DHT sensor
t = str(temp)
h = str(hum)
if waarde == 1:
	nu ='%d-%m-%y * %H:%M')
	nu ='%d-%m-%y   %H:%M')
Mess = "T:" + t + "C  H:" + h + "%"	
Mess = Mess + "\n" + nu

print "temp =", temp, "C\thumidity =", hum,"%" + "  -  " + nu


Gmail zendfunctie

Zendmail = 1
def ZendMail(body):

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

fromaddr = ""
toaddr = ""
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['SUbject'] = "Waarschuwing van Freddy's Raspberry"

server = smtplib.SMTP('',587)

Eerste maal bij opstarten


while True:
# Menu
if grovepi.digitalRead(switch) == 1:
menu_val = mySW()
print "Start menu: " + str(menu_val)

	# Tijd op dit ogenblik bepalen
	nu_min ='%M') #Minuten van de klok
	# Water en relay bediening
	pomp = grovepi.digitalRead(water)
	print 'Pomp: ' + str(pomp)
	if pomp == 1:
		Zendmail = 1
		if Zendmail == 1:
			boodschap = 'Geen water meer in de regenton.\nGelieve zo snel mogelijk bij te vullen.\nVerzonden op: '
			ZendMail(boodschap + nu)
			print 'E-mail: ' + boodschap + nu
			Zendmail = 0
			#stuur e-mail en stop de pomp
	# Uitlezen van de display om de minuut			
	if str(nu_min) &lt;&gt; str(beg_min):
		print 'Uitlezen naar display - ' + Mess
		beg_min = nu_min
except KeyboardInterrupt:
except (IOError,TypeError) as e:
	print "Error"


Hey Freddy,
Can you try running the program without the temp and humidity sensor and see if it is any better. A few people had reported problems with the Temperature and huidity sensor too. Can you also the script and check that you are running v1.2.2.



Hi Karan,

The problem remains.
I used the following program to generate a menu choice with 3 buttons and the lcd display.
Maybe you can try it.

In my case, when I start the program with a time.sleep delay of 0.5 and without touching anything, I have a read out of m_level1 (button menu) of 255.
This looks a bit wrong to me since it can either be 1 or 0.

I included the program.

Kind regards and many thanks for your support,



Hi Karan,

I did some testing on the temp/hum sensor.
It indeed has some instability.
Once a while the reading is ‘nan’ or it generates an Error as reading

Kind regards,


Hi Karan,

Again me. Sorry.
I have tested all analog and digital ports with temp/hum sensor, moisture sensor, switch on analog port.
The analog ports all give an error after a while and keep continuing working.
However the error that occurs occasionally are very annoying.
I tested all of this using the simple python scripts delivered by Grove.

Kind regards,


Hi Karan,

I forgot to mention…
I am running v 1.2.2

Kind regards,


Hey freddy,
For a lot of the users dacing the problem with analog reads and DHT sensor, the newer firmware solved the problem. You can update your GrovePi repository with a Git pull and then update the firmware to v1.2.5: using: avrdude -c gpio -p m328p -U flash:w:grove_pi_v1_2_5.cpp.hex. Then update the GrovePi python library here: by running: sudo python install. After this first try the firmware version script then try the DHT sensor and then try running the DHT sensor along with multiple sensors to see if it’s any better.



Hi Karan,

When I am in the GrovePi folder of my Raspberry and when I issue 'git pull, the following message appears:

'Error: Your local changes to the following files would be overwritten by merge: Script/’
Please, commit your changes or stash them before you can merge.

Because I am not very familiar with Linux, I do not know what to do next.
Can you please advise me a bit more in detail?
How do I get the necessary files on my Raspberry and in what folder should they reside.

Many thanks for your effort.

Kind regards,


Hi Karan,

After trying to do things, the DHT sensor stopped working completely and was no longer accessible.
Therefore I issued a new firmware update as indicated by the instructions from the web site.
Firmware version is now again v 1.2.2

So, I will need all the help I can get to install the 1.2.5 version

Kind regards,


Hey Freddy,
An easier way to test the new firmware and software would be to clone the GrovePi repository in another directory and try it out. If it works, then merge it with the main folder. Make a temporary folder on Desktop and clone the GrovePi repository: git clone Once this is done, open the GrovePi folder and follow the steps here:

Do let us know if this helps.



Hi Karan,

Following your instructions I was able to upgrade to the new version and it now reads v 1.2.5.
However, the bad news is that it did not solved the problem…
I still have read errors (‘nan’) when I use the temp/hum sensor and even when I simply use a switch on the analog port with a pull down resistor of 10 K.
I tried this with only a very small script to be sure that it is not due to my amateurish programming…

Also when I run my program I receive an error when reading ports.

Kind regards,



Hey Freddy,
Just wanted to make sure that you have run the script to install the new lGrovePi python library.

If you have done that already, can you try: and to see if these work properly. If these do work properly, can you try running the program attached here.



Hi Karan,

I did all that and have run the
The only thing I changed is the sensor port -> 7 and the sensor type to ‘0’ since my package came with the less professional sensor…

Once a while it produces ‘nan’ readings for both temperature and humidity.
I can trap these readings in my program.

However the following error is produced at the end:

Traceback (most recent call last):
File “grove”, line 44, in <module>
[temp,humidity] = grovepi.dht(sensor,0)
TypeError: ínt’object is not iterable

I hope this helps to solve the problem.
Is this reproducable in your lab?

Kind regards,


Hey Freddy,
We have seen the type errors a few times too. Using an try block and catching the TypeError usually makes it work. Can you try something like except TypeError: pass and see if it works. Have a look here on how to handle exceptions: .



Hi Karan,

Thanks for your reply.
It is comfortable for me to know that the errors are not only with me and that they occur also in the lab.
Of course now I am trapping the errors but normally, this should only be the exceptions and not to make a program work.
However, I can live with it.
I noticed that I get less errors if the system is up and running for a longer time.
One would expect the opposite, but it can only be an impression of me…
Thanks anyway for your continuous support.



So what is the root cause and solution since I got exact the same issue? A simple digitalRead of an input button gives unreliable results. For no reason it’s get high. I use the latest image and firmware.


Hey budy,
The IOErrors basically comes when the Pi misses a byte or reads a bad value from the GrovePi. It is pretty easy to handle. What you are describing is probably a bad sensor. Do you have any other sensors to try out.