Controlling solenoids with arduino


I am trying to control a solenoid valve with cayenne and arduino. I am trying to have the valve stay open when the button widget is selected and close when the widget is turned off. To the best of my knowledge, the button widget runs like a switch. It stays on until it is pressed again. I am just wondering how to get the solenoid to turn on when the button is pressed and turn off when it is pressed a second time

Do you have any code that works without Cayenne, or is this a fresh project? If you’re starting out fresh then it really depends on your solenoid. Can you post a model number? If you already have code then it will be super easy to merge everything. You are correct in that the button on the dashboard is only an on/off switch for now.

I am using the solenoid as a part of a smart greenhouse project I am working on. I am using the DHT11 temperature and humidity sensor and it is working fine. I can post the code for that if you would like but I do not have any other code for the solenoid. I am using THIS solenoid at the moment but that is subject to change. I am going to be using a TIP120 transistor to regulate the solenoid.

Cool, sounds like it should be a pretty simple project then. This page should be a pretty good resource for you.

I have the schematics figured out was just wondering about software. How do I tell the fan to turn off. I am using cayenne_out to control it. Does it just turn off when I turn off the button?

can you post the code .

as cayenne_out is send data to cayenne dashboard whereas cayenne_in is to receive data from cayenne dashboard.

I am sorry I am using out to push temp and in for the motor control. I just want to know if it will automatically turn off any code in the cayenne_in brackets or if I need a interupt circuit

you can use triggers for this. when temp reaches certain value turn on motor.

1 Like

not trying to engage at certain temperature. Need full manual control

@jerry can you provide more detail about what you are trying to do?

I am trying to have a button on the cayenne dashboard that opens a solonoid for a given amount of time than automaticly closes the solonoid and turns off the button. I have this code so far,

//recives solonoid control
if (CAYENNE_IN(V3) == HIGH){
digitalWrite(solenoidPin, HIGH); //opens the solonoid
delay(300000); //waits 5 minutes
digitalWrite(solenoidPin, LOW); //closes the solonoid
Cayenne.virtualWrite(V3, LOW); //turns off switch on cayenne

@jerry timer function is on cayenne roadmap and will be out soon. till then you got to do a bit long method to do this.
Switch button as pulse generator - #4 by shramik_salgaonkar
adjust the time according to your need.

If I want to have it just turn off when I turn off the button, How do I do that @shramik_salgaonkar?

Add a custom button widget and select channel as 1.

// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.

#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
int x;
// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

unsigned long lastMillis = 0;

void setup() {
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);

void loop() {
  int currentValue = getValue.asInt();
  if (currentValue == 1)

Virtual 1?

above code is for MQTT in which we use channel (1).