battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.

battle programmers allianceLog in

the LivinGrimoire Artificial General Intelligence software design pattern forum

descriptionArduino read temperature from LM35 heat detection sensor EmptyArduino read temperature from LM35 heat detection sensor

more_horiz
Arduino side:

Code:

/*
lm35 sketch
prints the temperature to the serial monitor
*/

const int inPin = 0; // analog pin

void setup() {
  Serial.begin(9600);
}

void loop() {
  int Value = analogRead(inPin);
  float millivolts = (value / 1024) * 5000;
  float celcius = millivolts /10;
  Serial.print(celsius);
  delay(1000);
}


laptop side:

Make sure you have the pyserial library installed. You can install it using pip:

pip install pyserial


Code:

import serial

# Adjust the port name to match your system
ser = serial.Serial('COM3', 9600)  # For Windows
# ser = serial.Serial('/dev/ttyUSB0', 9600)  # For Linux
# ser = serial.Serial('/dev/ttyACM0', 9600)  # For Linux with Arduino Uno

while True:
    if ser.in_waiting > 0:
        line = ser.readline().decode('utf-8').rstrip()
        print(line)


ser.readline().decode('utf-8').rstrip() reads one line of data from the serial buffer, which corresponds to the last complete line sent from the Arduino. Each call to ser.readline() retrieves the next line of data available in the buffer, so if the Arduino is continuously sending data, this command will read the most recent line that has been fully transmitted.

fez

If you want to ensure you’re reading the latest data, you might want to clear the buffer before starting to read, or handle the data in a way that processes only the most recent readings. Here’s an example of how you might clear the buffer before starting to read:

Code:

import serial
import time

# Adjust the port name to match your system
ser = serial.Serial('COM3', 9600)  # For Windows
# ser = serial.Serial('/dev/ttyUSB0', 9600)  # For Linux
# ser = serial.Serial('/dev/ttyACM0', 9600)  # For Linux with Arduino Uno

# Clear the buffer
ser.reset_input_buffer()

# Number of readings to take
num_readings = 10

for _ in range(num_readings):
    if ser.in_waiting > 0:
        line = ser.readline().decode('utf-8').rstrip()
        print(line)
    time.sleep(1)  # Delay between readings

ser.close()


This way, you start with a clear buffer and read only the most recent data sent from the Arduino.

descriptionArduino read temperature from LM35 heat detection sensor Emptyatexit python code beef up temperature class reader

more_horiz
atexit code upgrade

Code:

import serial
import time
import atexit

class SerialReader:
    def __init__(self, port='COM3', baud_rate=9600, timeout=1):
        self.ser = serial.Serial(port, baud_rate, timeout=timeout)
        atexit.register(self.close)  # Register the close method to be called on exit

    def read_serial_data(self, num_readings=10):
        readings = []
        for _ in range(num_readings):
            if self.ser.in_waiting > 0:
                line = self.ser.readline().decode('utf-8').rstrip()
                readings.append(line)
                print(line)
            time.sleep(1)  # Delay between readings
        return readings

    def close(self):
        self.ser.close()

# Example usage
if __name__ == '__main__':
    reader = SerialReader()
    data = reader.read_serial_data()
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply