Arduino - Touch Sensor - Solenoid Lock
We are going to learn how to use Arduino to control the solenoid lock in two use cases:
- Arduino turns on the solenoid lock if the touch sensor is touched and turns off the solenoid lock if the touch sensor is NOT touched.
- Arduino toggles the solenoid lock each time touch sensor is touched.
Hardware Required
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
About Touch Sensor and Solenoid Lock
If you do not know about solenoid lock and touch sensor (pinout, how it works, how to program ...), learn about them in the following tutorials:
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
Arduino Code - Touch Sensor controls Solenoid Lock
Quick Steps
- Connect Arduino to PC via USB cable
- Open Arduino IDE, select the right board and port
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- Touch and keep touching the touch sensor several seconds
- See the change of solenoid lock's state
Code Explanation
Read the line-by-line explanation in comment lines of source code!
Arduino Code - Touch Sensor Toggles Solenoid Lock
Quick Steps
- Connect Arduino to PC via USB cable
- Open Arduino IDE, select the right board and port
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- Keep touching the touch sensor several seconds and then release it.
- See the change of solenoid lock's state
Code Explanation
Read the line-by-line explanation in comment lines of code!
In the code, solenoidLockState = !solenoidLockState is equivalent to the following code:
Video Tutorial
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.