├── .gitignore ├── L293D.cpp ├── L293D.h ├── LICENSE.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | 24 | # Visual Studio 2015 cache/options directory 25 | .vs/ 26 | # Uncomment if you have tasks that create the project's static files in wwwroot 27 | #wwwroot/ 28 | 29 | # MSTest test Results 30 | [Tt]est[Rr]esult*/ 31 | [Bb]uild[Ll]og.* 32 | 33 | # NUNIT 34 | *.VisualState.xml 35 | TestResult.xml 36 | 37 | # Build Results of an ATL Project 38 | [Dd]ebugPS/ 39 | [Rr]eleasePS/ 40 | dlldata.c 41 | 42 | # DNX 43 | project.lock.json 44 | artifacts/ 45 | 46 | *_i.c 47 | *_p.c 48 | *_i.h 49 | *.ilk 50 | *.meta 51 | *.obj 52 | *.pch 53 | *.pdb 54 | *.pgc 55 | *.pgd 56 | *.rsp 57 | *.sbr 58 | *.tlb 59 | *.tli 60 | *.tlh 61 | *.tmp 62 | *.tmp_proj 63 | *.log 64 | *.vspscc 65 | *.vssscc 66 | .builds 67 | *.pidb 68 | *.svclog 69 | *.scc 70 | 71 | # Chutzpah Test files 72 | _Chutzpah* 73 | 74 | # Visual C++ cache files 75 | ipch/ 76 | *.aps 77 | *.ncb 78 | *.opendb 79 | *.opensdf 80 | *.sdf 81 | *.cachefile 82 | 83 | # Visual Studio profiler 84 | *.psess 85 | *.vsp 86 | *.vspx 87 | *.sap 88 | 89 | # TFS 2012 Local Workspace 90 | $tf/ 91 | 92 | # Guidance Automation Toolkit 93 | *.gpState 94 | 95 | # ReSharper is a .NET coding add-in 96 | _ReSharper*/ 97 | *.[Rr]e[Ss]harper 98 | *.DotSettings.user 99 | 100 | # JustCode is a .NET coding add-in 101 | .JustCode 102 | 103 | # TeamCity is a build add-in 104 | _TeamCity* 105 | 106 | # DotCover is a Code Coverage Tool 107 | *.dotCover 108 | 109 | # NCrunch 110 | _NCrunch_* 111 | .*crunch*.local.xml 112 | nCrunchTemp_* 113 | 114 | # MightyMoose 115 | *.mm.* 116 | AutoTest.Net/ 117 | 118 | # Web workbench (sass) 119 | .sass-cache/ 120 | 121 | # Installshield output folder 122 | [Ee]xpress/ 123 | 124 | # DocProject is a documentation generator add-in 125 | DocProject/buildhelp/ 126 | DocProject/Help/*.HxT 127 | DocProject/Help/*.HxC 128 | DocProject/Help/*.hhc 129 | DocProject/Help/*.hhk 130 | DocProject/Help/*.hhp 131 | DocProject/Help/Html2 132 | DocProject/Help/html 133 | 134 | # Click-Once directory 135 | publish/ 136 | 137 | # Publish Web Output 138 | *.[Pp]ublish.xml 139 | *.azurePubxml 140 | # TODO: Comment the next line if you want to checkin your web deploy settings 141 | # but database connection strings (with potential passwords) will be unencrypted 142 | *.pubxml 143 | *.publishproj 144 | 145 | # NuGet Packages 146 | *.nupkg 147 | # The packages folder can be ignored because of Package Restore 148 | **/packages/* 149 | # except build/, which is used as an MSBuild target. 150 | !**/packages/build/ 151 | # Uncomment if necessary however generally it will be regenerated when needed 152 | #!**/packages/repositories.config 153 | # NuGet v3's project.json files produces more ignoreable files 154 | *.nuget.props 155 | *.nuget.targets 156 | 157 | # Microsoft Azure Build Output 158 | csx/ 159 | *.build.csdef 160 | 161 | # Microsoft Azure Emulator 162 | ecf/ 163 | rcf/ 164 | 165 | # Microsoft Azure ApplicationInsights config file 166 | ApplicationInsights.config 167 | 168 | # Windows Store app package directory 169 | AppPackages/ 170 | BundleArtifacts/ 171 | 172 | # Visual Studio cache files 173 | # files ending in .cache can be ignored 174 | *.[Cc]ache 175 | # but keep track of directories ending in .cache 176 | !*.[Cc]ache/ 177 | 178 | # Others 179 | ClientBin/ 180 | ~$* 181 | *~ 182 | *.dbmdl 183 | *.dbproj.schemaview 184 | *.pfx 185 | *.publishsettings 186 | node_modules/ 187 | orleans.codegen.cs 188 | 189 | # RIA/Silverlight projects 190 | Generated_Code/ 191 | 192 | # Backup & report files from converting an old project file 193 | # to a newer Visual Studio version. Backup files are not needed, 194 | # because we have git ;-) 195 | _UpgradeReport_Files/ 196 | Backup*/ 197 | UpgradeLog*.XML 198 | UpgradeLog*.htm 199 | 200 | # SQL Server files 201 | *.mdf 202 | *.ldf 203 | 204 | # Business Intelligence projects 205 | *.rdl.data 206 | *.bim.layout 207 | *.bim_*.settings 208 | 209 | # Microsoft Fakes 210 | FakesAssemblies/ 211 | 212 | # GhostDoc plugin setting file 213 | *.GhostDoc.xml 214 | 215 | # Node.js Tools for Visual Studio 216 | .ntvs_analysis.dat 217 | 218 | # Visual Studio 6 build log 219 | *.plg 220 | 221 | # Visual Studio 6 workspace options file 222 | *.opt 223 | 224 | # Visual Studio LightSwitch build output 225 | **/*.HTMLClient/GeneratedArtifacts 226 | **/*.DesktopClient/GeneratedArtifacts 227 | **/*.DesktopClient/ModelManifest.xml 228 | **/*.Server/GeneratedArtifacts 229 | **/*.Server/ModelManifest.xml 230 | _Pvt_Extensions 231 | 232 | # Paket dependency manager 233 | .paket/paket.exe 234 | 235 | # FAKE - F# Make 236 | .fake/ -------------------------------------------------------------------------------- /L293D.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * L293D.cpp - Library for using a L293D motor controller chip 3 | * Created by Tyler Sweat, May 25 2015 4 | * Released under the MIT License 5 | */ 6 | 7 | #include "Arduino.h" 8 | #include "L293D.h" 9 | 10 | L293D::L293D(int _pin_E, int _pin_A, int _pin_B) 11 | { 12 | pinMode(_pin_E, OUTPUT); 13 | pinMode(_pin_A, OUTPUT); 14 | pinMode(_pin_B, OUTPUT); 15 | pin_E = _pin_E; 16 | pin_A = _pin_A; 17 | pin_B = _pin_B; 18 | // Set initially to 0 19 | set(0); 20 | } 21 | 22 | void L293D::set(double value) 23 | { 24 | if(value <= 1 && value >= -1) 25 | { 26 | set((int)(value * 255)); 27 | } 28 | } 29 | 30 | void L293D::set(int value) 31 | { 32 | if(value == 0) 33 | { 34 | digitalWrite(pin_E, LOW); 35 | val = value; 36 | } 37 | else if(value > 0 && value <= 255) 38 | { 39 | // disable before possibly switching directions 40 | digitalWrite(pin_E, LOW); 41 | // drive pin A HIGH and pin B LOW 42 | digitalWrite(pin_A, HIGH); 43 | digitalWrite(pin_B, LOW); 44 | // enable 45 | analogWrite(pin_E, value); 46 | // Save value it's been set to 47 | val = value; 48 | } 49 | else if(value < 0 && value >= -255) 50 | { 51 | // disable before possibly switching directions 52 | digitalWrite(pin_E, LOW); 53 | // drive pin A LOW and pin B HIGH 54 | digitalWrite(pin_A, LOW); 55 | digitalWrite(pin_B, HIGH); 56 | // enable 57 | analogWrite(pin_E, -1*value); // Multiply by -1 to make sure the value is positive. 58 | // Save value it's been set to 59 | val = value; 60 | } 61 | } 62 | 63 | int L293D::get() 64 | { 65 | return val; 66 | } 67 | -------------------------------------------------------------------------------- /L293D.h: -------------------------------------------------------------------------------- 1 | /* 2 | * L293D.h - Library for using a L293D motor controller chip 3 | * Created by Tyler Sweat, May 25 2015 4 | * Released under the MIT License 5 | */ 6 | 7 | #ifndef L293D_H 8 | #define L293D_H 9 | 10 | #include "Arduino.h" 11 | 12 | class L293D 13 | { 14 | public: 15 | L293D(int, int, int); 16 | void set(double); 17 | void set(int); 18 | int get(); 19 | private: 20 | int pin_E; 21 | int pin_A; 22 | int pin_B; 23 | int val; 24 | }; 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ty Sweat 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # arduino-L293D 2 | Arduino library for using a L293D motor controller chip 3 | 4 | ## Usage 5 | Download the source: 6 | 7 | ```git clone https://github.com/tylersweat/arduino-L293D.git``` 8 | 9 | and manually install the library. (http://www.arduino.cc/en/Guide/Libraries#toc5) 10 | 11 | 12 | #### Setting motor speed 13 | ``` 14 | motor.set(255); // Full on forward 15 | motor.set(1.0); // Also full on forward 16 | 17 | motor.set(-255); // Full on reverse 18 | motor.set(-1.0); // Also full on reverse 19 | ``` 20 | 21 | #### Wiring 22 | For controlling motor speed more than 100% on or off, you must use a PWM enabled pin on the Arduino for the L293D enable pin (ie: pins 3, 5, 6, 9, 10, and 11 on Arduino Uno). 23 | 24 | See the L293D datasheet for more information on connecting to the Arduino and to the motor. (http://www.ti.com/lit/ds/symlink/l293.pdf) 25 | 26 | ### Example Arduino sketch 27 | 28 | ``` 29 | #include 30 | 31 | L293D motor(9,8,7); 32 | 33 | setup() 34 | { 35 | } 36 | 37 | loop() 38 | { 39 | // Set motor speed and direction with a double value between -1 and 1 40 | motor.set(.75); // 75% power forward 41 | delay(2500); 42 | motor.set(-.3); // 30% power reverse 43 | delay(2500); 44 | // Set motor speed and direction with an int value between -255 and 255 45 | motor.set(200); 46 | delay(2500); 47 | motor.set(-150); 48 | delay(2500); 49 | 50 | // Use motor.get() to get current speed and then update it from that 51 | motor.set(-255); 52 | while(motor.get() != 255) 53 | { 54 | motor.set(motor.get() + 5); // Increment motor speed from previous speed 55 | delay(100); // Delay 100ms to see the speed changing 56 | } 57 | } 58 | ``` 59 | --------------------------------------------------------------------------------