Solenoids
Solenoids are basically electromagnets. Send current through it, and it becomes a magnet. Add an iron core in the shape of a piston, and you have a little actuator that pops the piston in or out when activated (solenoids come in two types: ‘push’ or ‘pull’). They can be expensive, and you will require a wall-wart transformer to have it work reliably. They can be found at local electronics supply stores or online.
Depending on their size, solenoids can draw a lot of current (>2A) when active, so bear that in mind if you want to keep a solenoid engaged for a long time. These can be use to push buttons, tap tuned plates, or pull very short distances (typically <2cm). There are also latching solenoids that ‘stick’ between the on/off state, those won’t be good for tapping, but are meant for fluid control valves. If you have a washing machine or dishwasher, you might have heard such solenoid valves in action, when the water supply is allowed to flow into the appliance.
In this recipe, we are using the N-Drive MOSFET and a plain button to turn the solenoid on/off. Notice how the N-Drive MOSFET can be used to toggle various different high-powered circuits – up to 60V/20A (relatively huge compared to the scale of prototypes we tend to work on here). In addition, because it has all the prerequisite resistors on the board itself to complete the MOSFET circuit, connecting it to most microcontrollers is very straightforward.
Wiring & Code
For a refresher on how to use the code in this recipe, click here.
For this recipe, we will use a pushbutton to trigger the activation of the solenoid.
-
Breadboard
Libraries Used
- none
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// code in this setup function runs just once, when Photon is powered up or reset void setup() { Serial.begin(115200); // Open a connection via the Serial port – useful for debugging pinMode(3, OUTPUT); // goes to N-Drive pinMode(2, INPUT_PULLUP); // goes to button delay(5000); // Common practice to allow board to 'settle' after connecting online } // code in this loop function runs forever, until you cut power! void loop() { updateSolenoid(); } void updateSolenoid() { int sensorReading = digitalRead(D1); if(sensorReading==HIGH) { digitalWrite(D0, HIGH); } else { digitalWrite(D0, LOW); } // add a tiny delay so as not to overwhelm the solenoid delay(10); }
-
Breadboard
We are using the 4xAA battery pack to power only the Photon; in this case 6V goes only to the Photon, and the solenoid itself is powered from the 12V/3A power supply via the MOSFET.
Libraries Used
- none
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// code in this setup function runs just once, when Photon is powered up or reset void setup() { Serial.begin(115200); // Open a connection via the Serial port – useful for debugging pinMode(D0, OUTPUT); // goes to N-Drive pinMode(D1, INPUT_PULLDOWN); // goes to button delay(5000); // Common practice to allow board to 'settle' after connecting online } // code in this loop function runs forever, until you cut power! void loop() { updateSolenoid(); } void updateSolenoid() { int sensorReading = digitalRead(D1); if(sensorReading==HIGH) { digitalWrite(D0, HIGH); } else { digitalWrite(D0, LOW); } // add a tiny delay so as not to overwhelm the solenoid delay(10); }