HC-SR04 sensor data not appearing in Python output.

Hi, I figured this would be the best place to post this. I have a HC-SR04 connected to an Arduino using this code, and I am passing the output over serial to my PC to be read by a python script.

This is my arduino code, standard ultrasonic stuff

int trig = 7;
int echo = 8;
int duration = 0;
int distance = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);

  digitalWrite(trig, LOW);
}

void loop() {
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);

  duration = pulseIn(echo, HIGH);
  distance = duration / 58.2;

  Serial.write(distance);
//  Serial.print(distance);
  delay(1000);
}

This is my little python script to read the data from the Arduino, it comes from a book Beginning Robotics with the Raspberry Pi and Arduino

import serial
import time

ser = serial.Serial('COM4', 9600)

while 1:
    recSer = ser.readline().decode('utf-8')
    recSer.rstrip()
    distance = int(recSer + '0')/10
    print("Distance: " + str(distance) + "cm ",    end = '\r')
    time.sleep(0.5)

What I am finding is when I run the Python script nothing appears in the shell where I would be expecting output to be. It is puzzling me as Arduino Serial monitor displays an output, the Python script compiles and runs but shows nothing. It is as if the data coming from the Arduino is going into some black hole.

When I tested a different program that just sent integers from the Arduino to the Python script data transfer worked fine. If anyone can spot anything I have done wrong, or can replicated it.

I am using Python 3.8.4

Use Serial.print() in your Arduino program.

...R
Simple Python - Arduino demo

Thanks for that but it doesn't work. I'm at a loss as I have seen that python code or a variation of it elsewhere too.

I read through your Python Arduino demo but do I need to make it that detailed for what I am wanting to do. Thing is I messed around with this a couple of years ago before life intervened and it worked, earlier version of python though that shouldn't matter.

Python script compiles and run but nothing it coming up on the screen. I am correct in thinking that the version of Python should not matter.

MagicalSputnik:
I read through your Python Arduino demo but do I need to make it that detailed for what I am wanting to do.

It would only take a few minutes of copy and paste to see if it helps?

...R

Fixed it,

I tried your code, Arduino and Python and that worked flawlessly. Then I saw in your python code

ser.read().decode('utf-8') where in the book and my code it is ser.readline().decode('utf-8')

So I change readline to read and voila it now outputs the Ultrasonic values.

However seems I spoke too soon. Values I get don't match with what Arduino pops out in the serial monitor. Sensor is pointed at my screen and Arduino serial monitor gives me a consistent 20.5cm confirmed by tape.

Python Shell output gives 10cm then 90cm and back again, flipping between these two values. Obviously wrong.

Why for example he uses Serial.write(), where as you suggested Serial.print which does push through the output. Why ser.readline() in python isn't working for me, but ser.read() is.

An lastly if I have ser.read() in Python script with Serial.write() in Arduino I get this

ValueError: invalid literal for int() with base 10: '\x120' I read up about the error and understand what it says but beating my head on the wall to fix it.

I'm frustrated when a book gives you code to follow but it doesn't work. Think I will just scrap it and use your examples.

MagicalSputnik:
So I change readline to read and voila it now outputs the Ultrasonic values.

Rather than make some haphazard changes to your code why not start with my working code and add a little bit to it to meet your additional requirement?

...R

I understand what you are saying. I did some digging and looked up about between ser.read() and ser.readline() as that was where the issue with his code seems to be. I put in the number of bytes to be read, in this case 2 ser.readline(2) and I am now getting sensible values, and I have figured something out for myself. The reason I persisted is I wanted to figure out why the code in the book wasn't working as expected.

However, I will also use your suggestion and modify your code it has features that I think will come in handy later on in my project and anything else.

MagicalSputnik:
and I have figured something out for myself. The reason I persisted is I wanted to figure out why the code in the book wasn't working as expected.

Good to hear. That is definitely worth doing.

...R