from machine import Pin, I2C, ADC
from ssd1306 import SSD1306_I2C
import utime
import math
# Configuración de hardware
sensor_voltaje = ADC(27)
conversion_factor = 3.3 / (65535)
WIDTH = 128 # Ancho de la pantalla OLED
HEIGHT = 64 # Alto de la pantalla OLED
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=200000) # Inicializa I2C usando los pines GP8 & GP9 (pines I2C0 predeterminados)
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c) # Inicializa la pantalla OLED
# Inicializar variables para la gráfica de tiempo
t = 0
x = [25, 25] # Coordenadas iniciales en x
y = [55, 55] # Coordenadas iniciales en y
def plot_time(yp, t, x, y, var=[0.0, 4.0], vpts=[25, 16, 40], hpts=[25, 55, 112]):
# Ejes
oled.vline(vpts[0], vpts[1], vpts[2], 1) # x, y, h
oled.hline(hpts[0], hpts[1], hpts[2], 1) # x, y, w
oled.text(str(round(var[0], 1)), vpts[0] - 25, hpts[1] - 5)
oled.text(str(round(var[1], 1)), vpts[0] - 25, vpts[1])
# Interpolación para el eje y
y[1] = int((yp - var[0]) / (var[1] - var[0]) * (vpts[1] - hpts[1]) + hpts[1])
if t < hpts[2] - hpts[0]:
x[1] = x[0] + 1
else:
x[1] = hpts[2]
# Dibujar la línea
oled.line(x[0], y[0], x[1], y[1], 1)
oled.show()
# Actualizar valores anteriores
y[0] = y[1]
x[0] = x[1]
# Si se ha alcanzado el final de la gráfica...
if t > hpts[2] - hpts[0]:
# Borra los primeros píxeles de la gráfica y el eje y
oled.fill_rect(vpts[0], vpts[1], 2, vpts[2], 0)
# Limpia toda la escala del eje y
oled.fill_rect(vpts[0] - 25, vpts[1], vpts[0], vpts[2] + 5, 0)
# Desplaza la gráfica un píxel a la izquierda
oled.scroll(-1, 0)
# Ejes
oled.vline(vpts[0], vpts[1], vpts[2], 1) # x, y, h
oled.hline(hpts[0], hpts[1], hpts[2], 1) # x, y, w
oled.text(str(round(var[0], 1)), vpts[0] - 25, hpts[1] - 5)
oled.text(str(round(var[1], 1)), vpts[0] - 25, vpts[1])
else:
t += 1
return t, x, y
def draw_arc(x0, y0, radius, start_angle, end_angle, step_angle):
for angle in range(start_angle, end_angle, step_angle):
x1 = x0 + int(radius * math.cos(math.radians(angle)))
y1 = y0 + int(radius * math.sin(math.radians(angle)))
oled.pixel(x1, y1, 1)
def draw_scale():
x0 = WIDTH // 2
y0 = HEIGHT // 2 + 8
radius = min(WIDTH, HEIGHT) // 2 - 8
scale_values = [0, 4.0]
for value in scale_values:
angle = -180 + ((value / 4) * 180) - 90
x1 = x0 + int((radius - 4) * math.cos(math.radians(angle)))
y1 = y0 + int((radius - 4) * math.sin(math.radians(angle)))
oled.text(str(value), x1, y1)
while True:
reading = ((sensor_voltaje.read_u16() * conversion_factor)/10)
print("mA: {:.2f} mA".format(reading)) # Imprime el valor de voltaje
oled.fill(0) # Limpia la pantalla OLED
# Calcular el ángulo para la aguja del tacómetro
angle = ((reading / (3.3)) * 180) - 90
# Calcular las coordenadas (x, y) del centro del arco
x0 = WIDTH // 2
y0 = HEIGHT // 2
# Calcular el radio del arco
radius = min(WIDTH, HEIGHT) // 2 - 8
# Dibujar la semiluna del tacómetro
draw_arc(x0, y0, radius, -180, 0, 1)
# Dibujar la aguja del tacómetro
x1 = x0 + int(radius * math.cos(math.radians(angle)))
y1 = y0 + int(radius * math.sin(math.radians(angle)))
oled.line(x0, y0, x1, y1, 1)
oled.text("mA: ", 30, 35)
oled.text(str(round(reading, 2)), 50,35)
draw_scale()
oled.show()
utime.sleep(3)
# Inicializar la gráfica de tiempo
t = 0
x = [25, 25]
y = [55, 55]
while t < 100:
volts = ((sensor_voltaje.read_u16() * conversion_factor)/10)
t, x, y = plot_time(volts, t, x, y)
oled.text("mA: ", 0, 0)
oled.text(str(round(volts, 2)), 90, 0)
oled.show()
utime.sleep(1) # Muestra un nuevo dato cada segundo