Updated 24 September 2020
Kits and Gadgets Electronics Inc.
Farwaniya, Kuwait 80003
Kuwait
ph: +965 99571844
kits
DRIVING A SOLENOID
Raspberry Pi can be programmed to activate or switch-ON many things from switches, LEDs, lamps, motors, buzzers and solenoids. It can be programmed also to output pulse of specific frequency. LEDs and some small lamps can be driven directly from GPIOs since it consumes less current within the output current range of the GPIOs. However, you cannot drive or wise to connect a solenoids directly to the GPIO since it could damage your RPI due to in-rush current, inductive kick, or excessive current when the solenoid is energized. You need to use a drive interface for such purpose to handle much higher current load.
The simple way to do it is by using a motor drive interface such as shown here. Instead of using it for driving a motor it will work also to drive a solenoid. So it has a double purpose! You can just connect Pin 2 and Pin 7 of L293 IC to RPI directly. The 12 Volts DC supply on Pin 8 depends on the load. So, if you use a 5 Volts solenoid you can connect it directly to 5 Volts DC line. Thereby, using only a single supply for L293 and RPI.
Below is the simple Solenoid PROGRAM that you can write using Notepad, Wordpad or MSWord. Just export this text file to RISCOS BBC BASIC preloaded on any Raspberry Pi and save it as BASIC program. This program can activate and release the solenoid once or continously. The control is through the standard keyboard connected on RPI USB port. However, I'm using a mini wireless keyboard when I wrote this program. There should be no problem since most keyboards uses the same standard ASCII code though BBC BASIC convert this to different code .
THE PROGRAM
10 REM This program will demonstrate activation of solenoid using motor drive.
20 REM Program for Raspberry Pi Demonstration Test Kit
30 REM Programmed by: Rolly Estomaguio
40 REM Created on: 12 May 2014
50 REM http://www.kits-gadgets.com
60 ON ERROR PRINT REPORT$;" at line ";ERL:END
70 PROCinit
80 PRINT "Press 'S' or 'E' to activate - ESC to finish. SPACEBAR or 'O' to cut-off "
90 GOTO 210
100 DEFPROCinit
110 OSCLI"RMEnsure GPIO 0.00 RMLoad GPIO"
120 OSCLI"RMensure GPIO 0.40 ERROR Please install the GPIO module"
130 SYS"GPIO_EnableI2C",0
140 SYS"GPIO_ExpAsGPIO",2
150 SYS"GPIO_GetBoard" TO PiType%
160 DIM A%(16)
170 IF PiType%=12 A%()= 3,4,17,27,22,10,9,11,7,8,25,23,15,14,24
180 REM OTHER GPIOs NOT ACTIVATED 2,18,28,29,30,31
190 ENDPROC
200 REPEAT
210 L$= GET$: OFF
220 IF L$= "O" OR L$= "o" THEN PROC_release
230 IF L$= "S" OR L$= "s" THEN PROC_sol
240 IF L$= "Q" OR L$= "q" THEN QUIT
250 IF L$= "R" OR L$= "r" THEN RUN
260 IF L$= "E" OR L$= "e" THEN SYS "GPIO_WriteData",24,1
270 GOTO 210
280 SYS "GPIO_WriteData",A%(N%),1
290 DEFPROC_release
300 FOR E%= 1 TO 16
310 SYS "GPIO_WriteData",A%(E%),0
320 NEXT
330 GOTO 210
340 DEFPROC_sol
350 K%= INKEY (1)
360 SYS "GPIO_WriteData",24,1: SYS "GPIO_WriteData",23,0
370 SYS "GPIO_WriteData",23,1: SYS "GPIO_WriteData",24,0
380 REM FOR D%= 1 TO 5000000: NEXT
390 IF INKEY (-99) THEN SYS "GPIO_WriteData",24,0: SYS "GPIO_WriteData",23,0: GOTO 210
400 FOR D%= 1 TO 1500000: NEXT
410 SYS "GPIO_WriteData",24,0
420 SYS "GPIO_WriteData",23,0
430 FOR D%= 1 TO 1000000: NEXT
440 PROC_sol
NOTE:
You can modify the program to elimate the use of GOTO command and LINE NUMBERS. Otherwise just enjoy it if it didn't bother you.
RUNNING THE PROGRAM:
To activate once and hold : Type " E"
To release : Type "O"
For continued ON & OFF : Type "S"
To stop : Press SPACEBAR
WHY SOLENOID?
You can do many things with the function of solenoid such as:
1. Activating electrical switches
2. Door or Window lock
3. Trigger for mechanical movement
4. Closing and opening liquid and gas valves
5. Automatic hole puncher and cutter
Copyright 2013 Kits and Gadgets Electronics Inc. All rights reserved.
Kits and Gadgets Electronics Inc.
Farwaniya, Kuwait 80003
Kuwait
ph: +965 99571844
kits