Python Programming Arduino Nano RP2040

May 25, 2022

arduino | nano | plc | epsicom | python

Python code example for using Nano RP2040 Connect with an EPSICOM-PLC NANO enclosure.

EPSICOM-PLC NANO Tech Specs

EPSICOM-PLC NANO turns any Arduino NANO into a PLC with robust input and output electronic interfaces.

  • Power supply:
    • 12÷24VDC power supply, with reverse polarity protection
  • Inputs/Outputs:
    • 4 x power relay outputs 10A / 240VAC (SPDT)
    • 4 x digital output open collector 4.5A / 350V
    • 4 x multi-mode inputs: digital or 0÷10V or 4÷10mA analog
    • 4 x optocouplers inputs
    • 4 x digital inputs or TTL level I/Os (SCL/SDA, RX/TX)
    • 1 x 0÷10V analog output
Let’s dive into coding

Will start by defining the inputs/outputs GPIOs into a python module called “epsicomplc”

import machine
from machine import Pin
from machine import PWM

class PLCPIN:

    # optocouple
    OPTO_1 = Pin(17, Pin.IN)
    OPTO_2 = Pin(16, Pin.IN)
    OPTO_3 = Pin(15, Pin.IN)
    OPTO_4 = Pin(25, Pin.IN)
    OPTO = [OPTO_1, OPTO_2, OPTO_3, OPTO_4]

    # relays
    REL_1 = Pin(28, Pin.OUT)
    REL_2 = Pin(29, Pin.OUT)
    REL_3 = Pin(4, Pin.OUT)
    REL_4 = Pin(7, Pin.OUT)
    REL = [REL_1, REL_2, REL_3, REL_4]

    # digital output
    OUT_1 = Pin(21, Pin.OUT)
    OUT_2 = Pin(20, Pin.OUT)
    OUT_3 = Pin(19, Pin.OUT)
    OUT_4 = Pin(18, Pin.OUT)
    OUT = [OUT_1, OUT_2, OUT_3, OUT_4]

    # digital input
    IN_1 = Pin(26, Pin.IN)
    IN_2 = Pin(27, Pin.IN)

    # analog input
    adc_pin = machine.Pin(26) # A0
    AD_1 = machine.ADC(adc_pin)
    adc_pin = machine.Pin(27) # A1
    AD_2 = machine.ADC(adc_pin)

    # pwm D10
    pwm_pin = Pin(5, Pin.OUT)
    PWM = PWM(pwm_pin)          # create a PWM object on a pin

class plc:
    def map(x, in_min, in_max, out_min, out_max):
        return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

comments powered by Disqus