05. Micro Python SW - Automated St. Andrews crossing
Hi,
![]() |
MicroPython board set |
Use the MicroPython SW bellow in your project. Same software controls LEDs, barrier and Buzzer, just need to connect it as explained before.
To load sw into pyboard first you need to copy code below in to a .txt on your PC using a notepad or simillar (I use notepad++). Save with name main.py
Connect board via USB cable to PC, when external device (pyboard) is detected load the file into the root of the pyboard. You override the empty file already in the board with same name.
Reset pyboard in the RST button.
Test and adjust as bellow the v1.0 of the Python code explained. Updated version on post 04. Train crossing (part 2) - Automated barrier
***
# turn on_off external LEDs, Servo and Buzzer on an external iR input, for a Train Crossing / St. Andrews Crossing
# Libraries
import time, pyb, machine, math
from pyb import Pin, Servo, DAC
from array import array
def ACT_PN(x):
pyb.LED(1).on() # internal LED(1) ON
led_1.high() # external LED(1) ON
led_2.low() # external LED(2) OFF
dac.write_timed(buf, 50 * len(buf), mode=DAC.CIRCULAR) # call sine wave from buffer at 50Hz
time.sleep(x) # leed sleep for xseg
pyb.LED(1).off() # internal LED(1) OFF
led_1.low() # external LED(1) OFF
led_2.high() # external LED(2) ON
dac.write(0) # Buzzer reset
time.sleep(x) # leed sleep for xseg
while True:
sw = pyb.Switch().value() # act internal USR switch
led_1 = Pin('Y11', Pin.OUT_PP) # set LED1 to out Y11
led_2 = Pin('Y12', Pin.OUT_PP) # set LED2 to out Y12
servo_1 = pyb.Servo(1) # set Servo_1 on position 1 (X1, VIN, GND)
servo_2 = pyb.Servo(2) # set Servo_2 on position 2 (X2, VIN, GND)
p_in = Pin('X4', Pin.IN, Pin.PULL_UP) # set X4 as external switch
iR = p_in.value() # read external iR sensor - Digital input (1/0)
# create a buffer containing a sine-wave
buf = bytearray(100)
for i in range(len(buf)):
buf[i] = 32 + int(32 * math.sin(2 * math.pi * i / len(buf)))
dac = DAC(1) # Set X5 as DAC for BUZZ - Digital to Analog output
# check status of iR input trigger for loop ativation
if iR == 0: # External iR sensor
servo_1.angle(-5,2000) # close PN servo in X1 (azimuth + speed)
servo_2.angle(0,2000) # close PN servo in X2 (azimuth + speed)
for y in range(3): # external iR
ACT_PN(1) # call ACT_PN loop
elif sw == True: # internal USR switch
servo_1.angle(-5,2000) # close PN servo in X1 (azimuth + speed)
servo_2.angle(0,2000) # close PN servo in X2 (azimuth + speed)
for y in range(3): # external iR
ACT_PN(1) # call ACT_PN loop
# set all to standard rest position
else:
servo_1.angle(-58,2000) # open PN servo in X1 (azimuth + speed)
servo_2.angle(-58,2000) # open PN servo in X2 (azimuth + speed)
led_2.low() # external LED2 RESET
pyb.LED(1).off() # internal LED2 RESET
pyb.LED(4).toggle() # internal LED(4) ON_OFF
time.sleep_ms(250)
***
Adjustments:
For fine tuning you need to adjust servo initial and final values (degrees) for the barrier due to installation differences on both servos.
Being -5º (servo 1) and 0º (servo 2) the closed position and -58º the fully opened position of both servos.
The correct values should be 0º and -58º (closed and opened position) but my servo 1 barrier was closing beyond the horizontal position, making to much pressure on plastic, so I had to level it to -5º. This is due to clip arm length being different each side, but it's adjustable once you test your set.
To adjust you edit your project in notepad++ and upload it again, once adjusted to your desired value.
The 2000 value is the speed of moving arm, meaning that it takes about two seconds to fully close barrier once activated. More less...
Closed/activated position for the Servo 1 and Servo 2:
![]() |
Modify these two loops (one loop for USR swith other for iR ativation) |
Open/initial position for the Servo 1 and Servo 2:
![]() |
Modify this loop to set default open position |
Comments
Post a Comment