celem

latchiing-relay-test-ino.txt

Aug 27th, 2021 (edited)
1,409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Test a single coil latching relay: KEMET EC2-3SNU
  3.   https://content.kemet.com/datasheets/KEM_R7002_EC2_EE2.pdf
  4.  
  5.   This test is using a 5V Arduino NANO but all other circuitry is 3V3.
  6.   Switching the Arduino output pins LOW applies an open collector ground
  7.   while switching the Arduino output pins to HIGH applies 3.3V via
  8.   external 20 ohm pullup resistors.
  9.  
  10.   NOTE: Since the contact load is resistive no snubber circuit is used.
  11.   NOTE: The EC2-3SNU set/reset Pulse Width requirement: > 10 ms
  12.  
  13.   CONNECTIONS:
  14.   NANO-2 -to- EC2-3SNU pin 1
  15.   NANO-3 -to- EC2-3SNU pin 12
  16.   LED-D2 -to- EC2-3SNU pin 3
  17.   LED-D1 -to- EC2-3SNU pin 5
  18.  
  19. */
  20.  
  21. #define COILP 2   // S+ (Pulse polarity to SET)
  22. #define COILN 3   // S- (Pulse polarity to SET)
  23. #define DEFAULT_PULSE 15  // Pulse Width: > 10 ms
  24.  
  25. int loops = 0;
  26. int Pulsewidth = 15;  // Pulsewidth in ms
  27.  
  28. void setup() {
  29.   int n;
  30.  
  31.   //start serial connection
  32.   Serial.begin(9600);
  33.  
  34.   // Start with relay coil pins with 3V3 applied via pullups
  35.   pinMode(COILP, OUTPUT);            // Relay coil pin 1
  36.   pinMode(COILN, OUTPUT);            // Relay coil pin 12
  37.   digitalWrite(COILP, HIGH);         // 3V3 applied via pullup
  38.   digitalWrite(COILN, HIGH);         // 3V3 applied via pullup
  39.  
  40.   pinMode(LED_BUILTIN, OUTPUT);      // Pin 13 on NANO
  41.   digitalWrite(LED_BUILTIN, LOW);    // Set the LED to off
  42.   digitalWrite(LED_BUILTIN, HIGH);   // Set the LED to on - alive blink
  43.   delay(50);
  44.   digitalWrite(LED_BUILTIN, LOW);    // Set the LED to off
  45.  
  46.  Serial.println("Press ENTER to start");
  47.   while (Serial.available() == 0) {
  48.     // Wait for User to Input Data
  49.   }
  50.   Serial.parseInt();  // Discard input (assumed to be an ENTER)
  51.  
  52.   // Offer user opportunity to temporarily change Pulsewidth
  53.   Serial.print("Pulsewidth is ");
  54.   Serial.print(Pulsewidth);
  55.   Serial.println(". Enter new value or just ENTER to keep current ?");
  56.   while (Serial.available() == 0) {
  57.     // Wait for User to Input Data
  58.   }
  59.   n = Serial.parseInt();
  60.   if(n >0)
  61.   {
  62.     Pulsewidth = n;
  63.   }
  64.     Serial.println("");
  65.     Serial.print("Pulsewidth is ");
  66.     Serial.println(Pulsewidth);
  67. }
  68.  
  69. void Relay_Quiescent()
  70. {
  71.     digitalWrite(COILP, HIGH);   // External pullup applies 3V3
  72.     digitalWrite(COILN, HIGH);   // External pullup applies 3V3
  73. }
  74.  
  75. void Relay_Set()
  76. {
  77.   digitalWrite(LED_BUILTIN, HIGH);  // Builtin LED to ON
  78.   digitalWrite(COILN, LOW);         // External pullup applies 3V3
  79.   digitalWrite(COILP, HIGH);        // Apply GND sink
  80.   delay(Pulsewidth);
  81.   Relay_Quiescent();
  82. }
  83.  
  84. void Relay_Reset()
  85. {
  86.   digitalWrite(LED_BUILTIN, LOW);   // Builtin LED to OFF
  87.   digitalWrite(COILN, HIGH);        // External pullup applies 3V3
  88.   digitalWrite(COILP, LOW);
  89.   delay(Pulsewidth);
  90.   Relay_Quiescent();
  91. }
  92.  
  93. /************************
  94.  * Main Endless Loop
  95.  ************************
  96.  */
  97. void loop() {
  98.   loops += 1;
  99.   Serial.print("Loop# ");
  100.   Serial.print(loops);
  101.   Serial.println(" iteration");
  102.  
  103.   // Set the relay
  104.   Serial.println("SETting relay");
  105.   Relay_Set();
  106.  
  107.   delay(3000);  // delay 3 seconds between set/reset
  108.  
  109.   Serial.println("RESETting relay");
  110.   Relay_Reset();
  111.  
  112.   delay(3000);  // delay 3 seconds between Reset/set
  113.  
  114. }
  115.  
Add Comment
Please, Sign In to add comment