Arduino, Relay and Pellet Feeder

I am working on a research project where I am utilizing E-Prime, a psychological programming software, to design and run cognitive experiments in primates. The basis of the study will be to have the primates touch shapes where an incorrect response give a timeout period on the touchscreen and a correct response elicits a pellet from the pellet feeder.

That being said, upon a correct response E-Prime has been instructed to send a String “a” to the Arduino, which is listening. When the Arduino receives the string it will pulse the relay, switching it HIGH and back LOW, to feed a pellet. The problem I am having is it correctly feeds a pellet when switch HIGH, but it also feeds a pellet when switched back LOW. I don’t need it to double feed.

Hardware:

  • Arduino Nano ATmega328P 5V
  • Breakout Board
  • 5V One Channel Relay w/ OPTO Isolation, High Low Lvl Trigger
  • Med-Associates 28V Pellet Feeder

I have also attached an image of my setup as well as a wiring diagram.

Here is the code I’m using:

/*E-Prime Sketch to incorporate pellet feeders into the UI*/

int incomingByte = 0;
int Relay8 = 13;

void setup()
{
  //Begin Serial
  Serial.begin(9600);
  
  //Debug statement to check if Arduino is resetting
  Serial.println("Start Code");
 
  //Pin 13 set to output
  pinMode(Relay8, OUTPUT);
}

void loop()
{
  if (Serial.available() > 0)
  {
    incomingByte = Serial.read();
    if(incomingByte == 'a')
    {
      // Activate the relay
      digitalWrite(Relay8, HIGH);
      Serial.println("Feeder ON");
      // Delay a bit
      delay(500);
      // De-activate relay
      digitalWrite(Relay8, LOW);
      Serial.println("Feeder OFF");
    }
  }
}

Wiring Diagram.pdf (91.2 KB)

Look at the state change detection tutorial to see how to detect edges. For instance, only feed a pellet after the LOW to HIGH transition and ignore the HIGH to LOW transition.

How would I go about implementing this into my current sketch? Rather green so I'm unsure of how to do that.

It sounds like the feeder dispenses whenever the relay state changes. So, don't bother with the delay and flipping it low again for a single pellet.

Keep a variable with the relay state in it (high or low). When you receive the 'a', flip the variable state and send it to the relay pin.

Then you dispense on each change.

I hope that your subjects will get tastier treats than the ones you're testing with

Currently that seems to be the case even though I don't understand it.

The current wiring shouldn't allow it to trigger again as the feeder should only be able to drop a pellet when the signal wire makes contact with GND. So when it switches LOW the relay is going back to NC where nothing is located breaking the connection between signal and GND. Attached above are images showing such.

If you could show me what you are talking about I would appreciate it. I have tried messing with my sketch, but can't figure out how to do what your suggesting.

They have unfortunately eaten worse!

/*E-Prime Sketch to incorporate pellet feeders into the UI*/

int incomingByte = 0;
int Relay8 = 13;
bool RelayOn=false;

void setup()
{
Serial.begin(9600);
Serial.println("Start Code");
pinMode(Relay8, OUTPUT);
}

void loop()
{
if (Serial.available() > 0)
  {
    incomingByte = Serial.read();
    if (incomingByte == 'a')
      {
      RelayOn=!RelayOn;
      Serial.println("Dispensing. Primates, enjoy your meal.");
      if(RelayOn)
        digitalWrite(Relay8, HIGH);
      else
        digitalWrite(Relay8, LOW);
    }
  }
}

Ah this is great, works almost perfectly.

What I am seeing is upon the state change from HIGH to LOW there seems to be a delay from when the relay switches to when the pellet feeder drops a pellet. It also doesn't trigger the feeder sometimes during that state change. EDIT: Upon further testing it only does this when you try to push 'a' through the serial monitor quickly. Therefore, this shouldn't be a problem since there will be a 5-10s delay between each trail in E-Prime, which triggers the setup.

Any reason why it would do this though?

I suspect that the problem is that your setup lets the input to the feeder float. It's supposed to dispense on a falling edge so I think that you need to provide 28V to the input from the relay and pull the input down to ground via a suitably sized resistor.

However, I note from the vendor's site that there are no prices, which no doubt means that the equipment is expensive, so I suggest that you get advice from someone who is more skilled with electronics than me.