jaarshad/Transducer_trainer
0
1import streamlit as st2import numpy as np3import matplotlib.pyplot as plt4 5st.set_page_config(page_title="Transducer Trainer", layout="centered")6st.title("🔧 Transducer Trainer")7transducer = st.selectbox(8 "Select a Transducer",9 ["Photodiode", "Phototransistor", "RTD", "LVDT", "NTC Thermistor", "Thermocouple", "PTC Thermistor", "Strain Gauge"]10)11 12def plot_graph(x, y, xlabel, ylabel, title):13 fig, ax = plt.subplots()14 ax.plot(x, y, marker='o')15 ax.set_xlabel(xlabel)16 ax.set_ylabel(ylabel)17 ax.set_title(title)18 ax.grid(True)19 st.pyplot(fig)20 21# --- Existing Transducers (as before, unchanged) ---22 23if transducer == "Photodiode":24 light_intensity = st.slider("Light Intensity (lux)", 0, 1000, 200)25 sensitivity = st.number_input("Sensitivity (µA/lux)", value=0.5)26 current = sensitivity * light_intensity27 voltage = current * 1000 # Assume 1kΩ load resistor28 st.write(f"Current = {current:.2f} µA")29 st.write(f"Voltage across 1kΩ = {voltage:.2f} mV")30 x = np.linspace(0, 1000, 50)31 y = sensitivity * x32 plot_graph(x, y, "Light Intensity (lux)", "Photocurrent (µA)", "Photodiode Response")33 34elif transducer == "Phototransistor":35 light_intensity = st.slider("Light Intensity (lux)", 0, 1000, 300)36 gain = st.number_input("Current Gain", value=100)37 base_current = 0.5 * light_intensity / 1000 # µA38 collector_current = gain * base_current39 voltage = collector_current * 1 # across 1Ω for example40 st.write(f"Collector Current = {collector_current:.2f} µA")41 st.write(f"Output Voltage = {voltage:.2f} mV")42 x = np.linspace(0, 1000, 50)43 y = gain * (0.5 * x / 1000)44 plot_graph(x, y, "Light Intensity (lux)", "Collector Current (µA)", "Phototransistor Response")45 46elif transducer == "RTD":47 temp = st.slider("Temperature (°C)", -50, 200, 25)48 R0 = st.number_input("Resistance at 0°C (Ω)", value=100)49 alpha = st.number_input("Temperature Coefficient (α)", value=0.00385)50 resistance = R0 * (1 + alpha * temp)51 voltage = resistance * 1 # 1A current52 st.write(f"Resistance = {resistance:.2f} Ω")53 st.write(f"Voltage = {voltage:.2f} V")54 x = np.linspace(-50, 200, 100)55 y = R0 * (1 + alpha * x)56 plot_graph(x, y, "Temperature (°C)", "Resistance (Ω)", "RTD Resistance vs Temperature")57 58elif transducer == "LVDT":59 displacement = st.slider("Displacement (mm)", -10, 10, 0)60 sensitivity = st.number_input("Sensitivity (V/mm)", value=0.2)61 voltage = sensitivity * displacement62 st.write(f"Output Voltage = {voltage:.2f} V")63 x = np.linspace(-10, 10, 50)64 y = sensitivity * x65 plot_graph(x, y, "Displacement (mm)", "Output Voltage (V)", "LVDT Output")66 67elif transducer == "NTC Thermistor":68 temp = st.slider("Temperature (°C)", -20, 150, 25)69 R25 = st.number_input("Resistance at 25°C (Ω)", value=10000)70 beta = st.number_input("Beta Value (K)", value=3950)71 T = temp + 273.1572 T0 = 25 + 273.1573 resistance = R25 * np.exp(beta * (1/T - 1/T0))74 voltage = resistance * 1e-3 # Assuming 1mA current75 st.write(f"Resistance = {resistance:.2f} Ω")76 st.write(f"Voltage = {voltage:.2f} V")77 x = np.linspace(-20, 150, 100)78 T_arr = x + 273.1579 y = R25 * np.exp(beta * (1/T_arr - 1/T0))80 plot_graph(x, y, "Temperature (°C)", "Resistance (Ω)", "NTC Resistance vs Temperature")81 82# --- Newly Added Transducers ---83 84elif transducer == "Thermocouple":85 temp = st.slider("Temperature (°C)", 0, 1000, 100)86 seebeck_coeff = st.number_input("Seebeck Coefficient (µV/°C)", value=41.0)87 voltage = seebeck_coeff * temp / 1000 # in mV88 st.write(f"Output Voltage = {voltage:.2f} mV")89 x = np.linspace(0, 1000, 100)90 y = seebeck_coeff * x / 100091 plot_graph(x, y, "Temperature (°C)", "Voltage (mV)", "Thermocouple Output")92 93elif transducer == "PTC Thermistor":94 temp = st.slider("Temperature (°C)", -50, 150, 25)95 R25 = st.number_input("Resistance at 25°C (Ω)", value=100)96 alpha = st.number_input("Temperature Coefficient (α)", value=0.05)97 resistance = R25 * (1 + alpha * (temp - 25))98 voltage = resistance * 1e-3 # Assuming 1mA current99 st.write(f"Resistance = {resistance:.2f} Ω")100 st.write(f"Voltage = {voltage:.2f} V")101 x = np.linspace(-50, 150, 100)102 y = R25 * (1 + alpha * (x - 25))103 plot_graph(x, y, "Temperature (°C)", "Resistance (Ω)", "PTC Resistance vs Temperature")104 105elif transducer == "Strain Gauge":106 strain = st.slider("Strain (μstrain)", -2000, 2000, 0)107 gauge_factor = st.number_input("Gauge Factor", value=2.0)108 resistance = 120 * (1 + gauge_factor * strain * 1e-6)109 st.write(f"Output Resistance = {resistance:.2f} Ω")110 x = np.linspace(-2000, 2000, 100)111 y = 120 * (1 + gauge_factor * x * 1e-6)112 plot_graph(x, y, "Strain (μstrain)", "Resistance (Ω)", "Strain Gauge Output")113 114 