Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to Arduino Programming

49,133 views

Published on

Getting started with Arduino Programming can be daunting. These are slides I used in my classes which introduced programming concepts to non-engineers, non-programmers, but totally people who wanted to learn more about electronics.

Published in: Engineering

Introduction to Arduino Programming

  1. 1. Arduino Programming (C) 2014 James Lewis james@baldengineer.com 1
  2. 2. Arduino Langauge 2 Machine Language (Binary Code) Assembly (Readable Code) C / C++ (Readable Code) C++ Libs C++ Libs C++ Libs “Arduino”
  3. 3. Arduino Langauge 3 Machine Language (Binary Code) Assembly (Readable Code) C / C++ (Readable Code) C++ Libs C++ Libs C++ Libs “Arduino”
  4. 4. Hello World (Blink) 4
  5. 5. Hello World (Blink) 5 Variable
  6. 6. Hello World (Blink) 6 Comments
  7. 7. Hello World (Blink) 7 Comments Good Comment: // Blue LED for Activity Indicator Bad Comment: // Pin 13
  8. 8. Hello World (Blink) 8 Functions
  9. 9. Hello World (Blink) 9 Functions
  10. 10. Hello World (Blink) 10 Instruction
  11. 11. Hello World (Blink) 11 Instruction
  12. 12. Hello World (Blink) 12 FunctionCall
  13. 13. Hello World (Blink) 13 Arguments
  14. 14. Hello World (Blink) 14
  15. 15. IDE Tools Menu 15
  16. 16. IDE Tools Menu 16
  17. 17. IDE Tools Menu 17
  18. 18. IDE Tools Menu 18
  19. 19. Arduino IDE 19 Type Stuff Here Compiler Output
  20. 20. Arduino IDE 20 Type Stuff Here Compiler Output Serial Monitor
  21. 21. Arduino IDE 21 Type Stuff Here Compiler Output Serial MonitorVerify & Upload
  22. 22. Arduino IDE 22 Type Stuff Here Compiler Output Serial MonitorVerify & Upload Board & Serial Port
  23. 23. Blink Exercise • Load the Blink Example and program it to your board
 • Change the values of delay() to see how it affects the behavior 23 Check the correct board and serial port are selected in the tools menu!
  24. 24. Hello World Serial Example 24
  25. 25. Serial objects 25
  26. 26. Serial objects 26 Enables Serial Baud Rate
  27. 27. Serial objects 27 Variables Strings Control Characters Print and Println
  28. 28. Serial objects 28 Variables Strings Control Characters Print and Println NOTE: Strings and Variables Can’t be used on the same line
  29. 29. Hello World (Serial) • Load up the serial code to the right
 • Exercise: • Change the 2000 in delay into a variable. • Print value of variable on same line as “Hello World” 29
  30. 30. Variables 30
  31. 31. How Much Memory is in your Arduino? 31
  32. 32. Variable Types 32 Bits Unsigned Range Signed Range byte 8 0 to 255 N/A char 8 0 to 255 ‘A’..’b’..’X’ N/A int 16 0 to 65535 -32,767 to 32,766 long 32 0 to 4,294,967,295 -2,147,483,648 to 2,147,483,647 float 32 ±3.4028235E+38 n/a double 32 n/a n/a
  33. 33. Variable Do and Don’t • DO Use Descriptive Names • “BlueLED”, “ActivityIndicator” • DON’T Use Bad Names • “Integer”, “Pin13” • DO Stick to a naming convention • Variables are Case Sensitive! • DON’T use same name for Local and Global Variables 33
  34. 34. Variable Scope 34
  35. 35. Variable Scope 35 Global
  36. 36. Variable Scope 36 Global Local to loop()
  37. 37. Variable Don’t! 37
  38. 38. Variable Don’t! 38
  39. 39. Variable Don’t! 39
  40. 40. analogReadings[] arrays 40 0 1 2 3 4 5
  41. 41. analogReadings[] arrays 41 0 1 2 3 4 5 Size Elements
  42. 42. analogReadings[] arrays 42 0 1 2 3 4 5 Size Elements arrays are 
 0-index based. 
 So last element
 is always 
 “1 less”
 than the size!
  43. 43. Pin Functions 43
  44. 44. pinMode() 44 Analog (A0..A5) Digital (0..13) INPUT Digital Input, Pull-Up Off Digital Input, Pull-Up Off INPUT_PULLUP Digital Input, Pull-Up On Digital Input, Pull-Up On OUTPUT Digital Output Digital Output Analog Pins can be used as Digital Pins pinMode(INPUT, Ax) isn’t necessary for analogRead()
  45. 45. digitalRead() & digitalWrite() 45
  46. 46. Pull-Up Resistor 46 INPUTs almost always need a Pull-Up or Pull-Down pinMode(INPUT_PULLUP) Turns on the Internal Pull-Up Resistor http://www.baldengineer.com/tutorials/arduino-pull-ups/
  47. 47. I/O Exercise • Objective: Understand Inputs and Outputs
 • Exercise: “Fix” the random LED 47
  48. 48. analogRead() 48 Analog Signal Convert Digital Value 10-bits, stored as 8-bits Values 0 to 1023 Voltage 0v to 5v 5 volts 1023 Steps = 4.887mV per Step Calling analogRead() on an Analog Pin, automatically converts to Input
  49. 49. analogWrite() 49 analogWrite() isn’t Analog (Except on the Due) Uno Pins: 3, 5, 6, 9, 10, 11 Actual AnalogPulse Width Modulation (PWM)
  50. 50. Analog Exercise • Connect Pin 6 to Analog 0
 Run this code
 • Then, disconnect from Pin 6 
 While Running the Code 50 Remember to open the Serial Monitor!
  51. 51. Analog Exercise • Connect Pin 6 to Analog 0
 Run this code
 • Then, disconnect from Pin 6 
 While Running the Code 51 Remember to open the Serial Monitor! A0, A1, A2..A5 are integers!
  52. 52. functions() 52
  53. 53. Functions Getting Data Back
  54. 54. Functions Getting Data Back Tip: The Arduino IDE, doesn’t require “prototyping”
  55. 55. Functions Getting Data Back Return Type Arguments “Return” FunctionName
  56. 56. Functions Returning Nothing 56 Return Type If the function doesn’t return anything, declare it as void
  57. 57. Function Exercise • “Re-Write” the built-in Blink Example to use a Function • Exercise: add a “argument” to adjust the delay time 57
  58. 58. Control Structures 58
  59. 59. if-statements 59
  60. 60. if-statements 60
  61. 61. if-statements 61
  62. 62. if-statements 62
  63. 63. control operators 63 == Equal to >
 >= Greater than (or equal) < <= Less Than (or equal) != Not Equal to || OR && AND | Bitwise OR & Bitwise AND
  64. 64. #1 if-statement mistake 64
  65. 65. #1 if-statement mistake 65 = != ==
  66. 66. for() loop example 66
  67. 67. for() loop 67
  68. 68. for() loop 68 Control Variable
  69. 69. for() loop 69 Control Variable Condition
  70. 70. for() loop 70 Control Variable Condition Increment
  71. 71. Array and For Exercise • Use an Array and two for- loops to read analog inputs, then display then
 • Notice the difference in brackets between the two loops 71
  72. 72. while() loop 72
  73. 73. while() loop 73 Condition
  74. 74. while() loop 74 Condition Loop conditions are same as “if conditions”
  75. 75. while() and Serial • This program echoes whatever is on the serial buffer back out 75
  76. 76. 76 More information?
 Visit
 baldengineer.com

×