Showing posts with label arduino. Show all posts
Showing posts with label arduino. Show all posts

Saturday, October 19, 2024

Multi Channel Analog data display on Python based GUI from Arduino Serial port



To create a Python Tkinter GUI that fetches many comma-separated values from a serial port and displays them we can use the pyserial library to handle the serial communication. Below is a simple example demonstrating how to achieve this. Make sure you have both tkinter and pyserial installed. You can install pyserial using pip if you haven't done so already:


The arduino code is simple modified version of available AnalogReadSerial example. In the code 4 AI channels are read and one DI channel is read. Their values are transmitted over Serial port. Note the COM port number of device and change accordingly in the code.


// the setup routine runs once when you press reset: void setup() {  // initialize serial communication at 9600 bits per second:  Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() {  // read the input on analog pin 0:  int Val1 = analogRead(A0);  int Val2 = analogRead(A1);  int Val3 = analogRead(A2);  int Val4 = analogRead(A3);  bool state = digitalRead(8);  // print out the value you read:  Serial.print(Val1);  Serial.print(Val2);  Serial.print(Val3);  Serial.print(Val4);  Serial.println(state);  delay(500);        // delay in between reads for stability }



Below is the Python script.

      
import tkinter as tk
import serial

# Serial port configuration
SERIAL_PORT = 'COM3'  # Change this to your serial port
BAUD_RATE = 9600

class SerialApp:
    def __init__(self, master):
        self.master = master
        self.master.title("Serial Data Display")
        
        # Create labels for each value
        self.labels = ['Val1:', 'Val2:', 'Val3:', 'Val4:']
        self.value_labels = []

        # Configure font
        label_font = ("Helvetica", 30, "bold")
        
        # Create and place labels in grid
        for i, label in enumerate(self.labels):
            lbl = tk.Label(master, text=label, font=label_font)
            lbl.grid(row=i, column=0, padx=20, pady=10)
            value_lbl = tk.Label(master, text="", font=("Helvetica", 30))
            value_lbl.grid(row=i, column=1, padx=20, pady=10)
            self.value_labels.append(value_lbl)

        # Open serial port
        self.serial_port = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1)

        # Update the GUI
        self.update()

    def update(self):
        try:
            # Read data from serial port
            line = self.serial_port.readline().decode('utf-8').strip()
            values = line.split(',')

            # Ensure we have four values
            if len(values) == 4:
                for value_lbl, value in zip(self.value_labels, values):
                    value_lbl.config(text=value)

        except Exception as e:
            print(f"Error: {e}")

        # Schedule the next update
        self.master.after(1000, self.update)

    def on_closing(self):
        self.serial_port.close()
        self.master.destroy()

if __name__ == "__main__":
    root = tk.Tk()
    app = SerialApp(root)

    # Handle window close event
    root.protocol("WM_DELETE_WINDOW", app.on_closing)

    root.mainloop()


The GUI will look as below



https://github.com/arihant122/Python-Tkinter-based-GUI-by-fetching-Serial-port-data-from-Arduino-


Monday, December 11, 2023

Arduino interfacing with ADS1115 for high resolution analog measurement

 


To interface with the ADS1115 ADC and print data on an LCD using Arduino, you will need to do the following:

  1. Connect the ADS1115 to the Arduino using the I2C protocol. Connect the SDA and SCL pins of the ADS1115 to the corresponding pins on the Arduino (A4 and A5, respectively).

  2. Connect the LCD to the Arduino using the appropriate pins. This can vary depending on the specific LCD you are using, but typically involves connecting the LCD's data pins (D4-D7) to digital pins on the Arduino and connecting the RS, E, and RW pins to other digital pins.

  3. Include the necessary libraries in your Arduino sketch. You will need the Wire library for I2C communication and the LiquidCrystal library for controlling the LCD.

  4. In your sketch, initialize the ADS1115 and the LCD. This will involve setting up the I2C communication and configuring the ADC and the LCD for your specific needs.

  5. Continuously read the data from the ADS1115 and display it on the LCD. You can use the ADS1115 library to read the data from the ADC, and the LiquidCrystal library to write the data to the LCD.

Here's some example code to get you started:

#include <Wire.h> #include <Adafruit_ADS1015.h> #include <LiquidCrystal.h> #define ADS1115_ADDRESS 0x48 Adafruit_ADS1115 ads(ADS1115_ADDRESS); #define LCD_RS 12 #define LCD_EN 11 #define LCD_D4 5 #define LCD_D5 4 #define LCD_D6 3 #define LCD_D7 2 LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7); void setup() { Wire.begin(); ads.begin(); lcd.begin(16, 2); } void loop() { int16_t adc0, adc1, adc2, adc3; adc0 = ads.readADC_SingleEnded(0); adc1 = ads.readADC_SingleEnded(1); adc2 = ads.readADC_SingleEnded(2); adc3 = ads.readADC_SingleEnded(3); lcd.clear(); lcd.setCursor(0, 0); lcd.print("ADC0: "); lcd.print(adc0); lcd.setCursor(0, 1); lcd.print("ADC1: "); lcd.print(adc1); delay(500); }

This code initializes the ADS1115 and the LCD in the setup() function, and continuously reads the data from the ADC and displays it on the LCD in the loop() function. The ADC data is read using the readADC_SingleEnded() function, and the data is written to the LCD using the print() function of the LiquidCrystal library. You can modify this code to suit your specific needs, such as changing the ADC channels that are read, or adding additional LCD displays.



#include <Wire.h> #include <Adafruit_ADS1015.h> #include <LiquidCrystal.h> #define ADS1115_ADDRESS 0x48 Adafruit_ADS1115 ads(ADS1115_ADDRESS); #define LCD_RS 12 #define LCD_EN 11 #define LCD_D4 5 #define LCD_D5 4 #define LCD_D6 3 #define LCD_D7 2 LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7); void setup() { Serial.begin(9600); Wire.begin(); ads.begin(); lcd.begin(16, 2); } void loop() { int16_t adc0, adc1, adc2, adc3; adc0 = ads.readADC_SingleEnded(0); adc1 = ads.readADC_SingleEnded(1); adc2 = ads.readADC_SingleEnded(2); adc3 = ads.readADC_SingleEnded(3); lcd.clear(); lcd.setCursor(0, 0); lcd.print("ADC0: "); lcd.print(adc0); lcd.setCursor(0, 1); lcd.print("ADC1: "); lcd.print(adc1); Serial.print("ADC0: "); Serial.println(adc0); Serial.print("ADC1: "); Serial.println(adc1); delay(500); }

This code adds the Serial.begin() function to initialize serial communication at a baud rate of 9600. In the loop() function, the ADC data is read and displayed on the LCD as before, but it is also sent over serial using the Serial.print() and Serial.println() functions. This allows you to monitor the ADC data in real-time using a serial monitor, such as the one built into the Arduino IDE. To view the serial output, open the serial monitor in the Arduino IDE and set the baud rate to 9600.

Difference between Active and Passive Transducer

  Feature Active Transducers Passive Transducers Power Supply Requires an external power source to operate...