from machine import Pin
from time import sleep_us, sleep_ms
import network
import time
import BlynkLib
# =====================================
# BLYNK CONFIG
# =====================================
BLYNK_TEMPLATE_NAME = "Connect Session"
BLYNK_AUTH_TOKEN = ""
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASS = ""
# =====================================
# MOTOR DRIVER PINS (A4988)
# =====================================
step_pin = Pin(14, Pin.OUT)
dir_pin = Pin(12, Pin.OUT)
enable_pin = Pin(13, Pin.OUT)
ms1 = Pin(26, Pin.OUT)
ms2 = Pin(27, Pin.OUT)
ms3 = Pin(21, Pin.OUT)
# =====================================
# INITIAL STATE
# =====================================
enable_pin.value(1) # Disable driver initially
dir_pin.value(1) # Clockwise direction
step_pin.value(0)
# Default mode = Full Step
ms1.value(0)
ms2.value(0)
ms3.value(0)
sleep_ms(50)
enable_pin.value(0) # Enable driver
# =====================================
# WIFI CONNECTION
# =====================================
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASS)
print("Connecting to WIFI ....")
while not wifi.isconnected():
time.sleep(0.1)
print("WIFI connected!")
# =====================================
# BLYNK CONNECTION
# =====================================
blynk = BlynkLib.Blynk(BLYNK_AUTH_TOKEN ,insecure=True)
# =====================================
# MOTOR FUNCTIONS
# =====================================
def set_mode(a, b, c):
enable_pin.value(1) # Disable driver
sleep_ms(10)
ms1.value(a)
ms2.value(b)
ms3.value(c)
dir_pin.value(1) # Clockwise
sleep_ms(10)
enable_pin.value(0) # Re-enable
sleep_ms(50)
def move_motor(steps):
dir_pin.value(1)
sleep_us(200)
for _ in range(steps):
step_pin.value(1)
sleep_us(800)
step_pin.value(0)
sleep_us(800)
# =====================================
# BLYNK EVENTS
# =====================================
@blynk.on("connected")
def blynk_connected():
print("Blynk Connected")
@blynk.on("V0") # @ for even handling
def v0_write(value):
if int(value[0]) ==1: # int bec it is send as an array or string
print("Full Step")
set_mode(0,0,0)
move_motor(50)
@blynk.on("V1") # @ for even handling
def v1_write(value):
if int(value[0]) ==1: # int bec it is send as an array or string
print("half Step")
set_mode(1,0,0)
move_motor(50)
@blynk.on("V2") # @ for even handling
def v2_write(value):
if int(value[0]) ==1: # int bec it is send as an array or string
print("1/4 Step")
set_mode(0,1,0)
move_motor(50)
@blynk.on("V3") # @ for even handling
def v3_write(value):
if int(value[0]) ==1: # int bec it is send as an array or string
print("1/8 Step")
set_mode(1,1,0)
move_motor(50)
@blynk.on("V4") # @ for even handling
def v4_write(value):
if int(value[0]) ==1: # int bec it is send as an array or string
print("1/16 Step")
set_mode(1,1,1)
move_motor(50)
# =====================================
# MAIN LOOP
# =====================================
while True:
blynk.run()