Ultrasonic sensor HC-SR04 with Python nanpy

Hi everybody,

First of all sorry for my bad english .

So I tried for hours the way to use my HC-SR04 with nanpy, but my script block when I want to read the echo return, and I really don’t know why ! Here my code:

#############################################################
#!/usr/bin/env python

Tire du site:

www.instructables.com/id/Simple-Arduino-and-HC-SR04-Example/?ALLSTEPS

This is the site for the schema.

from nanpy import ArduinoApi
from nanpy import SerialManager
from time import sleep
from time import time

connection = SerialManager()
a = ArduinoApi(connection=connection)

trig = 13
echo = 12
ledrouge = 11
ledbleu = 10

a.pinMode(trig, a.OUTPUT)
a.pinMode(echo, a.INPUT)
a.pinMode(ledrouge, a.OUTPUT)
a.pinMode(ledbleu, a.OUTPUT)

a.digitalWrite(trig, a.LOW)
sleep(0.5)
a.digitalWrite(trig, a.HIGH)
sleep(0.00001)
a.digitalWrite(trig, a.LOW)
start = time()

while a.digitalRead(echo) == 0:
start = time()

while a.digitalRead(echo) == 1:
stop = time()

elapsed = stop - start

distance = elapsed * 34000
distance = distance / 2

print “Distance : %.lf” % distance

if distance < 4:
a.digitalWrite(ledrouge, a.HIGH)
a.digitalWrite(ledbleu, a.LOW)
else:
a.digitalWrite(ledrouge, a.LOW)
a.digitalWrite(ledbleu, a.HIGH)

if distance >= 200 or distance <= 0:
print “Out of Range”
else:
print “Distance : %.lf cm” % distance

sleep(0.05)

###################################################

I think the code stop at “while a.digitalRead(echo) == 0:” I have to do a “Ctrl + C” to stop it.

Here the return of the “Ctrl + C”:

#########################
File “./UltraSon.py”, line 31, in
while a.digitalRead(echo) == 0:
File “/usr/local/lib/python2.7/dist-packages/nanpy-0.9-py2.7.egg/nanpy/arduinoboard.py”, line 55, in wrapper
return fconv(func(self, *args, **kwargs))
File “/usr/local/lib/python2.7/dist-packages/nanpy-0.9-py2.7.egg/nanpy/arduinoboard.py”, line 123, in wrapper
return _call(cls_name, 0, call_pars, connection=connection)
File “/usr/local/lib/python2.7/dist-packages/nanpy-0.9-py2.7.egg/nanpy/arduinoboard.py”, line 28, in _call
_write(id, connection)
File “/usr/local/lib/python2.7/dist-packages/nanpy-0.9-py2.7.egg/nanpy/arduinoboard.py”, line 9, in _write
connection.write(’%s\0’ % str(data))
File “/usr/local/lib/python2.7/dist-packages/nanpy-0.9-py2.7.egg/nanpy/serialmanager.py”, line 87, in write
self._serial.write(value)
File “/usr/lib/python2.7/dist-packages/serial/serialposix.py”, line 460, in write
def write(self, data):
KeyboardInterrupt
########################################

I know that a lot of you will tell that I have to use the arduino language but I want to learn with Python :grin:
I thank you for all of you that read my post, and if some of you can think about the problem with me you are REALLY welcome XD

Have a good night,
And keep going !!
Aurélien

What are you running this code on?
You can not run python on an arduino.
Read the sticky post about how to use this forum, it tells you how to post code correctly.

Is ther any code running on the arduino?

Morning Grumpy_Mike,

>What are you running this code on?
I am running this code from a raspberry pi with nanpy.

> You can not run python on an arduino.
With this python module you can run python on the Arduino, so that's why I try to code with Python :roll_eyes:

> Read the sticky post about how to use this forum, it tells you how to post code correctly.
Sorry 'bout this, it was very late for me when I posted and it was my first time, I will not do it again :blush:

> Is ther any code running on the arduino?
Yes, the nanpy one, so that you can send the python code directly in the arduino and it can interprete the code immediatly without compiling !

Have a nice day,

Yes, the nanpy one, so that you can send the python code directly in the arduino and it can interprete the code immediatly without compiling !

No Python dose not run on the Arduino. It is a system for controlling the arduino from python. The arduino runs some sort of Firmetta code that just responds to serial commands from the Pi. The full install instructions are here:-

http://www.raspberrypi.org/forums/viewtopic.php?t=46881&p=546921

This is nothing to do with an Arduino and therefore does not belong on this forum.

Ok no problem,

I see that this forum is helpfull and everyone is welcome !!

So if I want some info on arduino I will go somewhere else !!

Bye !!

I see that this forum is helpfull and everyone is welcome !!

Yes it is if they have an Arduino related problem.

However your problem has absolutely nothing to do with the arduino.

So if I want some info on arduino I will go somewhere else !!

You are misunderstanding what that package does, it has nothing to do with the Arduino.
Your error messages are generated by the Raspberry Pi, not the Arduino.

Your problem is with the Raspberry Pi, so ask in that forum.