Module 6 Sensing and Actuation with Arduino PDF

Summary

This document is an educational module on sensing and actuation using Arduino. It covers topics such as digital and analog input/output, DC and Servo motors, and interfacing various sensors with Arduino. The module provides code examples and Fritzing diagrams.

Full Transcript

Module 6: Sensing and Actuation with Arduino 6.1 Digital and Analog Write and Read with Arduino 2 6.1.1 Digital Input to Control an LED 2 6.1.2 Analog Output to Control the Brightness of an LED...

Module 6: Sensing and Actuation with Arduino 6.1 Digital and Analog Write and Read with Arduino 2 6.1.1 Digital Input to Control an LED 2 6.1.2 Analog Output to Control the Brightness of an LED 5 Pulse Width Modulation (PWM) 5 6.1.3 Analog Input and Output to Control the Brightness of an LED 7 6.1.4 Using Analog input Pins as Digital I/O Pins 9 6.2 DC and Servo Motors 11 6.2.1 Standard DC or Toy Motors 12 6.2.2 DC Gearhead Motors 13 1) ServoCity DC gearhead motor (in the MEC101 Mechatronics kit): 14 2) Pololu DC gearhead motor (Not in the kit): 15 Powering DC Motors in the Kit 16 6.2.3 Standard Hobby Servo Motors 16 1. FS5103B Standard Servo (not in the kit) 16 2. FS90R continuous rotation micro servo motors (in the kit) 17 3. Standard Micro Servo 29EXIS1123 (in the kit) 19 6.3 DC and Servo Motor Control using Arduino 20 6.3.1 Controlling DC motors using H-bridge Integrated Circuit (IC) Chip 20 6.3.2 Controlling Finite Rotation Servo Motors 26 Servo Troubles 29 6.3.3 Controlling Continuous Rotation Servo Motors 31 6.4 Interfacing Sensors with Arduino 35 6.4.1 Photocells and Thermistors 35 6.4.2 Limit Switches 39 6.4.3 Ultrasonic Sensor 41 6.4.4 PIR Motion Sensor 47 6.4.5 Infrared (IR) Line Sensor 49 6.4.6 Potentiometer 55 6.4.7 TMP36 Sensor 58 Copyright Dr. Anurag Purwar, [email protected] May not be distributed or disseminated without permission. 1 6.1 Digital and Analog Write and Read with Arduino 6.1.1 Digital Input to Control an LED We have previously seen how to light up a simple output device LED to turn on and off at a certain rate. In this section, we will see how we can control its behavior using a momentary switch. We would like to create a basic circuit using an LED and a switch and then use the pressing of the switch as an event to trigger a desired behavior from the LED. Consider the Fritzing diagram as shown in Fig. 9.6.1 below. In this circuit diagram, there is a resistor in series with the switch and the voltage is being read at the junction of the two by an Arduino Uno at pin number 2. An LED is connected at pin 13 where there is a built-in resistor to protect the LED. Figure 6.1.1: Physical connection to read an input from the switch Figure 6.1.2 shows the schematic for the switch part of the circuit. Copyright Dr. Anurag Purwar, [email protected] May not be distributed or disseminated without permission. 2 Figure 6.1.2: Schematic for the Switch Circuit We need a resistor here because in the absence of one, when the switch is pressed, there would be a short circuit -- connecting the 5V line and GND side of Arduino together. When the switch is open, no current flows through the circuit -- we say it is an open circuit -- and the junction point is directly connected to GND, which is at 0 V. Arduino reads it as a LOW voltage. When the switch is pressed, all the 5V is dumped across the 10k Ohm resistor and the junction point is now connected to the +5V line. Arduino reads it as a HIGH voltage. Now, we can use these readings to affect turning on or off of the LED. 1. int switchPin = 2; // the number of the switch pin 2. int ledPin = 13; // the number of the LED pin 3. int switchState = 0; // variable for reading the state of the switch 4. 5. void setup() 6. { 7. pinMode(ledPin, OUTPUT); 8. pinMode(switchPin , INPUT); 9. } 10. 11. void loop() 12. { 13. switchState = digitalRead(switchPin); // read the state of the switch: 14. 15. if (switchState == HIGH) 16. { 17. digitalWrite(ledPin, HIGH); 18. } Copyright Dr. Anurag Purwar, [email protected] May not be distributed or disseminated without permission. 3 19. else 20. { 21. digitalWrite(ledPin, LOW); // turn off the LED 22. } 23. 24. } A similar code is available in the Arduino IDE at File → Examples → Digital → Button. What if one wanted to turn the LED off when the switch was pressed and turn it on when the switch was not pressed? This would be easy to do in the program simply by changing the digitalWrite statements or by changing the if conditionals. What if one wanted to blink the LED five times at 1s interval when the switch was pressed and then turn it off when the switch was not pressed? This could be accomplished by modifying the code to the following: 1. int switchPin = 2; // the number of the switch pin 2. int ledPin = 13; // the number of the LED pin 3. int switchState = 0; // variable for reading the state of the switch 4. 5. void setup() 6. { 7. pinMode(ledPin, OUTPUT); 8. pinMode(switchPin , INPUT); 9. } 10. 11. void loop() 12. { 13. switchState = digitalRead(buttonPin); // read the state of the switch: 14. 15. // check if the switch is pressed. If it is, the switchState is HIGH and we blink the LED 16. if (switchState == HIGH) 17. { 18. for (int i =0; i

Use Quizgecko on...
Browser
Browser