Arduino – DC Motor ”; Previous Next In this chapter, we will interface different types of motors with the Arduino board (UNO) and show you how to connect the motor and drive it from your board. There are three different type of motors − DC motor Servo motor Stepper motor A DC motor (Direct Current motor) is the most common type of motor. DC motors normally have just two leads, one positive and one negative. If you connect these two leads directly to a battery, the motor will rotate. If you switch the leads, the motor will rotate in the opposite direction. Warning − Do not drive the motor directly from Arduino board pins. This may damage the board. Use a driver Circuit or an IC. We will divide this chapter into three parts − Just make your motor spin Control motor speed Control the direction of the spin of DC motor Components Required You will need the following components − 1x Arduino UNO board 1x PN2222 Transistor 1x Small 6V DC Motor 1x 1N4001 diode 1x 270 Ω Resistor Procedure Follow the circuit diagram and make the connections as shown in the image given below. Precautions Take the following precautions while making the connections. First, make sure that the transistor is connected in the right way. The flat side of the transistor should face the Arduino board as shown in the arrangement. Second, the striped end of the diode should be towards the +5V power line according to the arrangement shown in the image. Spin ControlArduino Code int motorPin = 3; void setup() { } void loop() { digitalWrite(motorPin, HIGH); } Code to Note The transistor acts like a switch, controlling the power to the motor. Arduino pin 3 is used to turn the transistor on and off and is given the name ”motorPin” in the sketch. Result Motor will spin in full speed when the Arduino pin number 3 goes high. Motor Speed Control Following is the schematic diagram of a DC motor, connected to the Arduino board. Arduino Code int motorPin = 9; void setup() { pinMode(motorPin, OUTPUT); Serial.begin(9600); while (! Serial); Serial.println(“Speed 0 to 255″); } void loop() { if (Serial.available()) { int speed = Serial.parseInt(); if (speed >= 0 && speed <= 255) { analogWrite(motorPin, speed); } } } Code to Note The transistor acts like a switch, controlling the power of the motor. Arduino pin 3 is used to turn the transistor on and off and is given the name ”motorPin” in the sketch. When the program starts, it prompts you to give the values to control the speed of the motor. You need to enter a value between 0 and 255 in the Serial Monitor. In the ”loop” function, the command ”Serial.parseInt” is used to read the number entered as text in the Serial Monitor and convert it into an ”int”. You can type any number here. The ”if” statement in the next line simply does an analog write with this number, if the number is between 0 and 255. Result The DC motor will spin with different speeds according to the value (0 to 250) received via the serial port. Spin Direction Control To control the direction of the spin of DC motor, without interchanging the leads, you can use a circuit called an H-Bridge. An H-bridge is an electronic circuit that can drive the motor in both directions. H-bridges are used in many different applications. One of the most common application is to control motors in robots. It is called an H-bridge because it uses four transistors connected in such a way that the schematic diagram looks like an “H.” We will be using the L298 H-Bridge IC here. The L298 can control the speed and direction of DC motors and stepper motors, and can control two motors simultaneously. Its current rating is 2A for each motor. At these currents, however, you will need to use heat sinks. Components Required You will need the following components − 1 × L298 bridge IC 1 × DC motor 1 × Arduino UNO 1 × breadboard 10 × jumper wires Procedure Following is the schematic diagram of the DC motor interface to Arduino Uno board. The above diagram shows how to connect the L298 IC to control two motors. There are three input pins for each motor, Input1 (IN1), Input2 (IN2), and Enable1 (EN1) for Motor1 and Input3, Input4, and Enable2 for Motor2. Since we will be controlling only one motor in this example, we will connect the Arduino to IN1 (pin 5), IN2 (pin 7), and Enable1 (pin 6) of the L298 IC. Pins 5 and 7 are digital, i.e. ON or OFF inputs, while pin 6 needs a pulse-width modulated (PWM) signal to control the motor speed. The following table shows which direction the motor will turn based on the digital values of IN1 and IN2. IN1 IN2 Motor Behavior BRAKE 1 FORWARD 1 BACKWARD 1 1 BRAKE Pin IN1 of the IC L298 is connected to pin 8 of Arduino while IN2 is connected to pin 9. These two digital pins of Arduino control the direction of the motor. The EN A pin of IC is connected to the PWM pin 2 of Arduino. This will control the speed of the motor. To set the values of Arduino pins 8 and 9, we have used the digitalWrite() function, and to set the value of pin 2, we have to use the analogWrite() function. Connection Steps Connect 5V and the ground of the IC to 5V and the ground of Arduino, respectively. Connect the motor to pins 2 and 3 of the IC. Connect IN1 of the IC to pin 8 of Arduino. Connect IN2 of the IC to pin 9 of Arduino. Connect EN1 of IC to pin 2 of Arduino. Connect SENS A pin of IC to the ground. Connect Arduino using Arduino USB cable and upload the program to Arduino using Arduino IDE software. Provide
Category: arduino
Arduino – Water Detector / Sensor ”; Previous Next Water sensor brick is designed for water detection, which can be widely used in sensing rainfall, water level, and even liquid leakage. Connecting a water sensor to an Arduino is a great way to detect a leak, spill, flood, rain, etc. It can be used to detect the presence, the level, the volume and/or the absence of water. While this could be used to remind you to water your plants, there is a better Grove sensor for that. The sensor has an array of exposed traces, which read LOW when water is detected. In this chapter, we will connect the water sensor to Digital Pin 8 on Arduino, and will enlist the very handy LED to help identify when the water sensor comes into contact with a source of water. Components Required You will need the following components − 1 × Breadboard 1 × Arduino Uno R3 1 × Water Sensor 1 × led 1 × 330 ohm resistor Procedure Follow the circuit diagram and hook up the components on the breadboard as shown in the image given below. Sketch Open the Arduino IDE software on your computer. Coding in the Arduino language will control your circuit. Open a new sketch File by clicking on New. Arduino Code #define Grove_Water_Sensor 8 // Attach Water sensor to Arduino Digital Pin 8 #define LED 9 // Attach an LED to Digital Pin 9 (or use onboard LED) void setup() { pinMode(Grove_Water_Sensor, INPUT); // The Water Sensor is an Input pinMode(LED, OUTPUT); // The LED is an Output } void loop() { /* The water sensor will switch LOW when water is detected. Get the Arduino to illuminate the LED and activate the buzzer when water is detected, and switch both off when no water is present */ if( digitalRead(Grove_Water_Sensor) == LOW) { digitalWrite(LED,HIGH); }else { digitalWrite(LED,LOW); } } Code to Note Water sensor has three terminals – S, Vout(+), and GND (-). Connect the sensor as follows − Connect the +Vs to +5v on your Arduino board. Connect S to digital pin number 8 on Arduino board. Connect GND with GND on Arduino. Connect LED to digital pin number 9 in Arduino board. When the sensor detects water, pin 8 on Arduino becomes LOW and then the LED on Arduino is turned ON. Result You will see the indication LED turn ON when the sensor detects water. Print Page Previous Next Advertisements ”;
Arduino – Math Library
Arduino – Math Library ”; Previous Next The Arduino Math library (math.h) includes a number of useful mathematical functions for manipulating floating-point numbers. Library Macros Following are the macros defined in the header math.h − Given below is the list of macros defined in the header math.h Macros Value Description M_E 2.7182818284590452354 The constant e. M_LOG2E 1.4426950408889634074 /* log_2 e */ The logarithm of the e to base 2 M_1_PI 0.31830988618379067154 /* 1/pi */ The constant 1/pi M_2_PI 0.63661977236758134308 /* 2/pi */ The constant 2/pi M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */ The constant 2/sqrt(pi) M_LN10 2.30258509299404568402 /* log_e 10 */ The natural logarithm of the 10 M_LN2 0.69314718055994530942 /* log_e 2 */ The natural logarithm of the 2 M_LOG10E 0.43429448190325182765 /* log_10 e */ The logarithm of the e to base 10 M_PI 3.14159265358979323846 /* pi */ The constant pi M_PI_2 3.3V1.57079632679489661923 /* pi/2 */ The constant pi/2 M_PI_4 0.78539816339744830962 /* pi/4 */ The constant pi/4 M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */ The constant 1/sqrt(2) M_SQRT2 1.41421356237309504880 /* sqrt(2) */ The square root of 2 acosf – The alias for acos() function asinf – The alias for asin() function atan2f – The alias for atan2() function cbrtf – The alias for cbrt() function ceilf – The alias for ceil() function copysignf – The alias for copysign() function coshf – The alias for cosh() function expf – The alias for exp() function fabsf – The alias for fabs() function fdimf – The alias for fdim() function floorf – The alias for floor() function fmaxf – The alias for fmax() function fminf – The alias for fmin() function fmodf – The alias for fmod() function frexpf – The alias for frexp() function hypotf – The alias for hypot() function INFINITY – INFINITY constant isfinitef – The alias for isfinite() function isinff – The alias for isinf() function isnanf – The alias for isnan() function ldexpf – The alias for ldexp() function log10f – The alias for log10() function logf – The alias for log() function lrintf – The alias for lrint() function lroundf – The alias for lround() function Library Functions The following functions are defined in the header math.h − Given below is the list of functions are defined in the header math.h S.No. Library Function & Description 1 double acos (double __x) The acos() function computes the principal value of the arc cosine of __x. The returned value is in the range [0, pi] radians. A domain error occurs for arguments not in the range [-1, +1]. 2 double asin (double __x) The asin() function computes the principal value of the arc sine of __x. The returned value is in the range [-pi/2, pi/2] radians. A domain error occurs for arguments not in the range [-1, +1]. 3 double atan (double __x) The atan() function computes the principal value of the arc tangent of __x. The returned value is in the range [-pi/2, pi/2] radians. 4 double atan2 (double __y, double __x) The atan2() function computes the principal value of the arc tangent of __y / __x, using the signs of both arguments to determine the quadrant of the return value. The returned value is in the range [-pi, +pi] radians. 5 double cbrt (double __x) The cbrt() function returns the cube root of __x. 6 double ceil (double __x) The ceil() function returns the smallest integral value greater than or equal to __x, expressed as a floating-point number. 7 static double copysign (double __x, double __y) The copysign() function returns __x but with the sign of __y. They work even if __x or __y are NaN or zero. 8 double cos(double __x) The cos() function returns the cosine of __x, measured in radians. 9 double cosh (double __x) The cosh() function returns the hyperbolic cosine of __x. 10 double exp (double __x) The exp() function returns the exponential value of __x. 11 double fabs (double __x) The fabs() function computes the absolute value of a floating-point number __x. 12 double fdim (double __x, double __y) The fdim() function returns max(__x – __y, 0). If __x or __y or both are NaN, NaN is returned. 13 double floor (double __x) The floor() function returns the largest integral value less than or equal to __x, expressed as a floating-point number. 14 double fma (double __x, double __y, double __z) The fma() function performs floating-point multiply-add. This is the operation (__x * __y) + __z, but the intermediate result is not rounded to the destination type. This can sometimes improve the precision of a calculation. 15 double fmax (double __x, double __y) The fmax() function returns the greater of the two values __x and __y. If an argument is NaN, the other argument is returned. If both the arguments are NaN, NaN is returned. 16 double fmin (double __x, double __y) The fmin() function returns the lesser of the two values __x and __y. If an argument is NaN, the other argument is returned. If both the arguments are NaN, NaN is returned. 17 double fmod (double __x, double__y) The function fmod() returns the floating-point remainder of __x / __y. 18 double frexp (double __x, int * __pexp) The frexp() function breaks a floating-point number into a normalized fraction and an integral power of 2. It stores the integer in the int object pointed to by __pexp. If __x is a normal float point number, the frexp() function returns the value v, such that v has a magnitude in the interval [1/2, 1) or zero, and __x equals v times 2 raised to the power __pexp. If __x is zero, both parts of the result are zero. If __x is not a finite number, the frexp() returns __x as is and stores 0 by __pexp. Note − This implementation permits a zero pointer as a directive to skip a storing the exponent. 19 double hypot (double __x, double__y) The hypot() function returns sqrt(__x*__x + __y*__y). This is the length of the hypotenuse of a right triangle with sides of length
Arduino – Quick Guide
Arduino – Quick Guide ”; Previous Next Arduino – Overview Arduino is a prototype platform (open-source) based on an easy-to-use hardware and software. It consists of a circuit board, which can be programed (referred to as a microcontroller) and a ready-made software called Arduino IDE (Integrated Development Environment), which is used to write and upload the computer code to the physical board. The key features are − Arduino boards are able to read analog or digital input signals from different sensors and turn it into an output such as activating a motor, turning LED on/off, connect to the cloud and many other actions. You can control your board functions by sending a set of instructions to the microcontroller on the board via Arduino IDE (referred to as uploading software). Unlike most previous programmable circuit boards, Arduino does not need an extra piece of hardware (called a programmer) in order to load a new code onto the board. You can simply use a USB cable. Additionally, the Arduino IDE uses a simplified version of C++, making it easier to learn to program. Finally, Arduino provides a standard form factor that breaks the functions of the micro-controller into a more accessible package. Board Types Various kinds of Arduino boards are available depending on different microcontrollers used. However, all Arduino boards have one thing in common: they are programed through the Arduino IDE. The differences are based on the number of inputs and outputs (the number of sensors, LEDs, and buttons you can use on a single board), speed, operating voltage, form factor etc. Some boards are designed to be embedded and have no programming interface (hardware), which you would need to buy separately. Some can run directly from a 3.7V battery, others need at least 5V. Here is a list of different Arduino boards available. Arduino boards based on ATMEGA328 microcontroller Board Name Operating Volt Clock Speed Digital i/o Analog Inputs PWM UART Programming Interface Arduino Uno R3 5V 16MHz 14 6 6 1 USB via ATMega16U2 Arduino Uno R3 SMD 5V 16MHz 14 6 6 1 USB via ATMega16U2 Red Board 5V 16MHz 14 6 6 1 USB via FTDI Arduino Pro 3.3v/8 MHz 3.3V 8MHz 14 6 6 1 FTDI-Compatible Header Arduino Pro 5V/16MHz 5V 16MHz 14 6 6 1 FTDI-Compatible Header Arduino mini 05 5V 16MHz 14 8 6 1 FTDI-Compatible Header Arduino Pro mini 3.3v/8mhz 3.3V 8MHz 14 8 6 1 FTDI-Compatible Header Arduino Pro mini 5v/16mhz 5V 16MHz 14 8 6 1 FTDI-Compatible Header Arduino Ethernet 5V 16MHz 14 6 6 1 FTDI-Compatible Header Arduino Fio 3.3V 8MHz 14 8 6 1 FTDI-Compatible Header LilyPad Arduino 328 main board 3.3V 8MHz 14 6 6 1 FTDI-Compatible Header LilyPad Arduino simple board 3.3V 8MHz 9 4 5 0 FTDI-Compatible Header Arduino boards based on ATMEGA32u4 microcontroller Board Name Operating Volt Clock Speed Digital i/o Analog Inputs PWM UART Programming Interface Arduino Leonardo 5V 16MHz 20 12 7 1 Native USB Pro micro 5V/16MHz 5V 16MHz 14 6 6 1 Native USB Pro micro 3.3V/8MHz 5V 16MHz 14 6 6 1 Native USB LilyPad Arduino USB 3.3V 8MHz 14 6 6 1 Native USB Arduino boards based on ATMEGA2560 microcontroller Board Name Operating Volt Clock Speed Digital i/o Analog Inputs PWM UART Programming Interface Arduino Mega 2560 R3 5V 16MHz 54 16 14 4 USB via ATMega16U2B Mega Pro 3.3V 3.3V 8MHz 54 16 14 4 FTDI-Compatible Header Mega Pro 5V 5V 16MHz 54 16 14 4 FTDI-Compatible Header Mega Pro Mini 3.3V 3.3V 8MHz 54 16 14 4 FTDI-Compatible Header Arduino boards based on AT91SAM3X8E microcontroller Board Name Operating Volt Clock Speed Digital i/o Analog Inputs PWM UART Programming Interface Arduino Mega 2560 R3 3.3V 84MHz 54 12 12 4 USB native Arduino – Board Description In this chapter, we will learn about the different components on the Arduino board. We will study the Arduino UNO board because it is the most popular board in the Arduino board family. In addition, it is the best board to get started with electronics and coding. Some boards look a bit different from the one given below, but most Arduinos have majority of these components in common. Power USB Arduino board can be powered by using the USB cable from your computer. All you need to do is connect the USB cable to the USB connection (1). Power (Barrel Jack) Arduino boards can be powered directly from the AC mains power supply by connecting it to the Barrel Jack (2). Voltage Regulator The function of the voltage regulator is to control the voltage given to the Arduino board and stabilize the DC voltages used by the processor and other elements. Crystal Oscillator The crystal oscillator helps Arduino in dealing with time issues. How does Arduino calculate time? The answer is, by using the crystal oscillator. The number printed on top of the Arduino crystal is 16.000H9H. It tells us that the frequency is 16,000,000 Hertz or 16 MHz. Arduino Reset You can reset your Arduino board, i.e., start your program from the beginning. You can reset the UNO board in two ways. First, by using the reset button (17) on the board. Second, you can connect an external reset button to the Arduino pin labelled RESET (5). Pins (3.3, 5, GND, Vin) 3.3V (6) − Supply 3.3 output volt 5V (7) − Supply 5 output volt Most of the components used with Arduino board works fine with 3.3 volt and 5 volt. GND (8)(Ground) − There are several GND pins on the Arduino, any of which can be used to ground your circuit. Vin (9) − This pin also can be used to power the Arduino board from an external power source, like AC mains power supply. Analog pins The Arduino UNO board has six analog input pins A0 through A5. These pins can read the signal from an analog sensor like the humidity sensor or temperature sensor and convert it into a digital value that can
Arduino – Useful Resources
Arduino – Useful Resources ”; Previous Next The following resources contain additional information on Arduino. Please use them to get more in-depth knowledge on this topic. Useful Video Courses Arduino based Text to Speech (TTS) Converter 12 Lectures 1 hours Ashraf Said More Detail Arduino meets Python: Step by Step 13 Lectures 42 mins Ashraf Said More Detail Arduino Communication with SPI Protocol 13 Lectures 1.5 hours Ashraf Said More Detail Arduino Interrupt: Step by Step Guide with Practical Example 17 Lectures 1 hours Ashraf Said More Detail Arduino Email Sending Motion Detector 12 Lectures 53 mins Ashraf Said More Detail Arduino SMS Sending Motion Detector using Python 17 Lectures 1 hours Ashraf Said More Detail Print Page Previous Next Advertisements ”;
Arduino – Communication
Arduino – Communication ”; Previous Next Hundreds of communication protocols have been defined to achieve this data exchange. Each protocol can be categorized into one of the two categories: parallel or serial. Parallel Communication Parallel connection between the Arduino and peripherals via input/output ports is the ideal solution for shorter distances up to several meters. However, in other cases when it is necessary to establish communication between two devices for longer distances it is not possible to use parallel connection. Parallel interfaces transfer multiple bits at the same time. They usually require buses of data – transmitting across eight, sixteen, or more wires. Data is transferred in huge, crashing waves of 1’s and 0’s. Advantages and Drawbacks of Parallel Communication Parallel communication certainly has its advantages. It is faster than serial, straightforward, and relatively easy to implement. However, it requires many input/output (I/O) ports and lines. If you have ever had to move a project from a basic Arduino Uno to a Mega, you know that the I/O lines on a microprocessor can be precious and few. Therefore, we prefer serial communication, sacrificing potential speed for pin real estate. Serial Communication Modules Today, most Arduino boards are built with several different systems for serial communication as standard equipment. Which of these systems are used depends on the following factors − How many devices the microcontroller has to exchange data with? How fast the data exchange has to be? What is the distance between these devices? Is it necessary to send and receive data simultaneously? One of the most important things concerning serial communication is the Protocol, which should be strictly observed. It is a set of rules, which must be applied such that the devices can correctly interpret data they mutually exchange. Fortunately, Arduino automatically takes care of this, so that the work of the programmer/user is reduced to simple write (data to be sent) and read (received data). Types of Serial Communications Serial communication can be further classified as − Synchronous − Devices that are synchronized use the same clock and their timing is in synchronization with each other. Asynchronous − Devices that are asynchronous have their own clocks and are triggered by the output of the previous state. It is easy to find out if a device is synchronous or not. If the same clock is given to all the connected devices, then they are synchronous. If there is no clock line, it is asynchronous. For example, UART (Universal Asynchronous Receiver Transmitter) module is asynchronous. The asynchronous serial protocol has a number of built-in rules. These rules are nothing but mechanisms that help ensure robust and error-free data transfers. These mechanisms, which we get for eschewing the external clock signal, are − Synchronization bits Data bits Parity bits Baud rate Synchronization Bits The synchronization bits are two or three special bits transferred with each packet of data. They are the start bit and the stop bit(s). True to their name, these bits mark the beginning and the end of a packet respectively. There is always only one start bit, but the number of stop bits is configurable to either one or two (though it is normally left at one). The start bit is always indicated by an idle data line going from 1 to 0, while the stop bit(s) will transition back to the idle state by holding the line at 1. Data Bits The amount of data in each packet can be set to any size from 5 to 9 bits. Certainly, the standard data size is your basic 8-bit byte, but other sizes have their uses. A 7-bit data packet can be more efficient than 8, especially if you are just transferring 7-bit ASCII characters. Parity Bits The user can select whether there should be a parity bit or not, and if yes, whether the parity should be odd or even. The parity bit is 0 if the number of 1’s among the data bits is even. Odd parity is just the opposite. Baud Rate The term baud rate is used to denote the number of bits transferred per second [bps]. Note that it refers to bits, not bytes. It is usually required by the protocol that each byte is transferred along with several control bits. It means that one byte in serial data stream may consist of 11 bits. For example, if the baud rate is 300 bps then maximum 37 and minimum 27 bytes may be transferred per second. Arduino UART The following code will make Arduino send hello world when it starts up. void setup() { Serial.begin(9600); //set up serial library baud rate to 9600 Serial.println(“hello world”); //print hello world } void loop() { } After the Arduino sketch has been uploaded to Arduino, open the Serial monitor at the top right section of Arduino IDE. Type anything into the top box of the Serial Monitor and press send or enter on your keyboard. This will send a series of bytes to the Arduino. The following code returns whatever it receives as an input. The following code will make Arduino deliver output depending on the input provided. void setup() { Serial.begin(9600); //set up serial library baud rate to 9600 } void loop() { if(Serial.available()) //if number of bytes (characters) available for reading from { serial port Serial.print(“I received:”); //print I received Serial.write(Serial.read()); //send what you read } } Notice that Serial.print and Serial.println will send back the actual ASCII code, whereas Serial.write will send back the actual text. See ASCII codes for more information. Print Page Previous Next Advertisements ”;
Arduino – Servo Motor
Arduino – Servo Motor ”; Previous Next A Servo Motor is a small device that has an output shaft. This shaft can be positioned to specific angular positions by sending the servo a coded signal. As long as the coded signal exists on the input line, the servo will maintain the angular position of the shaft. If the coded signal changes, the angular position of the shaft changes. In practice, servos are used in radio-controlled airplanes to position control surfaces like the elevators and rudders. They are also used in radio-controlled cars, puppets, and of course, robots. Servos are extremely useful in robotics. The motors are small, have built-in control circuitry, and are extremely powerful for their size. A standard servo such as the Futaba S-148 has 42 oz/inches of torque, which is strong for its size. It also draws power proportional to the mechanical load. A lightly loaded servo, therefore, does not consume much energy. The guts of a servo motor is shown in the following picture. You can see the control circuitry, the motor, a set of gears, and the case. You can also see the 3 wires that connect to the outside world. One is for power (+5volts), ground, and the white wire is the control wire. Working of a Servo Motor The servo motor has some control circuits and a potentiometer (a variable resistor, aka pot) connected to the output shaft. In the picture above, the pot can be seen on the right side of the circuit board. This pot allows the control circuitry to monitor the current angle of the servo motor. If the shaft is at the correct angle, then the motor shuts off. If the circuit finds that the angle is not correct, it will turn the motor until it is at a desired angle. The output shaft of the servo is capable of traveling somewhere around 180 degrees. Usually, it is somewhere in the 210-degree range, however, it varies depending on the manufacturer. A normal servo is used to control an angular motion of 0 to 180 degrees. It is mechanically not capable of turning any farther due to a mechanical stop built on to the main output gear. The power applied to the motor is proportional to the distance it needs to travel. So, if the shaft needs to turn a large distance, the motor will run at full speed. If it needs to turn only a small amount, the motor will run at a slower speed. This is called proportional control. How Do You Communicate the Angle at Which the Servo Should Turn? The control wire is used to communicate the angle. The angle is determined by the duration of a pulse that is applied to the control wire. This is called Pulse Coded Modulation. The servo expects to see a pulse every 20 milliseconds (.02 seconds). The length of the pulse will determine how far the motor turns. A 1.5 millisecond pulse, for example, will make the motor turn to the 90-degree position (often called as the neutral position). If the pulse is shorter than 1.5 milliseconds, then the motor will turn the shaft closer to 0 degrees. If the pulse is longer than 1.5 milliseconds, the shaft turns closer to 180 degrees. Components Required You will need the following components − 1 × Arduino UNO board 1 × Servo Motor 1 × ULN2003 driving IC 1 × 10 KΩ Resistor Procedure Follow the circuit diagram and make the connections as shown in the image given below. Sketch Open the Arduino IDE software on your computer. Coding in the Arduino language will control your circuit. Open a new sketch File by clicking on New. Arduino Code /* Controlling a servo position using a potentiometer (variable resistor) */ #include <Servo.h> Servo myservo; // create servo object to control a servo int potpin = 0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180) myservo.write(val); // sets the servo position according to the scaled value delay(15); } Code to Note Servo motors have three terminals – power, ground, and signal. The power wire is typically red, and should be connected to the 5V pin on the Arduino. The ground wire is typically black or brown and should be connected to one terminal of ULN2003 IC (10 -16). To protect your Arduino board from damage, you will need some driver IC to do that. Here we have used ULN2003 IC to drive the servo motor. The signal pin is typically yellow or orange and should be connected to Arduino pin number 9. Connecting the Potentiometer A voltage divider/potential divider are resistors in a series circuit that scale the output voltage to a particular ratio of the input voltage applied. Following is the circuit diagram − $$V_{out} = (V_{in} times R_{2})/ (R_{1} + R_{2})$$ Vout is the output potential, which depends on the applied input voltage (Vin) and resistors (R1 and R2) in the series. It means that the current flowing through R1 will also flow through R2 without being divided. In the above equation, as the value of R2 changes, the Vout scales accordingly with respect to the input voltage, Vin. Typically, a potentiometer is a potential divider, which can scale the output voltage of the circuit based on the value of the variable resistor, which is scaled using the knob. It has three pins: GND, Signal, and +5V as shown in the diagram below − Result By changing the pot’s NOP position, servo motor will change its angle. Print Page Previous
Arduino – Advanced I/O Function ”; Previous Next In this chapter, we will learn some advanced Input and Output Functions. analogReference() Function Configures the reference voltage used for analog input (i.e. the value used as the top of the input range). The options are − DEFAULT − The default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards) INTERNAL − An built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8 (not available on the Arduino Mega) INTERNAL1V1 − A built-in 1.1V reference (Arduino Mega only) INTERNAL2V56 − A built-in 2.56V reference (Arduino Mega only) EXTERNAL − The voltage applied to the AREF pin (0 to 5V only) is used as the reference analogReference() Function Syntax analogReference (type); type − can use any type of the follow (DEFAULT, INTERNAL, INTERNAL1V1, INTERNAL2V56, EXTERNAL) Do not use anything less than 0V or more than 5V for external reference voltage on the AREF pin. If you are using an external reference on the AREF pin, you must set the analog reference to EXTERNAL before calling the analogRead() function. Otherwise, you will short the active reference voltage (internally generated) and the AREF pin, possibly damaging the microcontroller on your Arduino board. Alternatively, you can connect the external reference voltage to the AREF pin through a 5K resistor, allowing you to switch between external and internal reference voltages. Note that the resistor will alter the voltage that is used as the reference because there is an internal 32K resistor on the AREF pin. The two act as a voltage divider. For example, 2.5V applied through the resistor will yield 2.5 * 32 / (32 + 5) = ~2.2V at the AREF pin. Example int analogPin = 3;// potentiometer wiper (middle terminal) connected to analog pin 3 int val = 0; // variable to store the read value void setup() { Serial.begin(9600); // setup serial analogReference(EXTERNAL); // the voltage applied to the AREF pin (0 to 5V only) // is used as the reference. } void loop() { val = analogRead(analogPin); // read the input pin Serial.println(val); // debug value } Print Page Previous Next Advertisements ”;
Arduino – Random Numbers
Arduino – Random Numbers ”; Previous Next To generate random numbers, you can use Arduino random number functions. We have two functions − randomSeed(seed) random() randomSeed (seed) The function randomSeed(seed) resets Arduino’s pseudorandom number generator. Although the distribution of the numbers returned by random() is essentially random, the sequence is predictable. You should reset the generator to some random value. If you have an unconnected analog pin, it might pick up random noise from the surrounding environment. These may be radio waves, cosmic rays, electromagnetic interference from cell phones, fluorescent lights and so on. Example randomSeed(analogRead(5)); // randomize using noise from analog pin 5 random( ) The random function generates pseudo-random numbers. Following is the syntax. random( ) Statements Syntax long random(max) // it generate random numbers from 0 to max long random(min, max) // it generate random numbers from min to max Example long randNumber; void setup() { Serial.begin(9600); // if analog input pin 0 is unconnected, random analog // noise will cause the call to randomSeed() to generate // different seed numbers each time the sketch runs. // randomSeed() will then shuffle the random function. randomSeed(analogRead(0)); } void loop() { // print a random number from 0 to 299 Serial.print(“random1=”); randNumber = random(300); Serial.println(randNumber); // print a random number from 0to 299 Serial.print(“random2=”); randNumber = random(10, 20);// print a random number from 10 to 19 Serial.println (randNumber); delay(50); } Let us now refresh our knowledge on some of the basic concepts such as bits and bytes. Bits A bit is just a binary digit. The binary system uses two digits, 0 and 1. Similar to the decimal number system, in which digits of a number do not have the same value, the ‘significance’ of a bit depends on its position in the binary number. For example, digits in the decimal number 666 are the same, but have different values. Bytes A byte consists of eight bits. If a bit is a digit, it is logical that bytes represent numbers. All mathematical operations can be performed upon them. The digits in a byte do not have the same significance either. The leftmost bit has the greatest value called the Most Significant Bit (MSB). The rightmost bit has the least value and is therefore, called the Least Significant Bit (LSB). Since eight zeros and ones of one byte can be combined in 256 different ways, the largest decimal number that can be represented by one byte is 255 (one combination represents a zero). Print Page Previous Next Advertisements ”;
Arduino – Overview
Arduino – Overview ”; Previous Next Arduino is a prototype platform (open-source) based on an easy-to-use hardware and software. It consists of a circuit board, which can be programed (referred to as a microcontroller) and a ready-made software called Arduino IDE (Integrated Development Environment), which is used to write and upload the computer code to the physical board. The key features are − Arduino boards are able to read analog or digital input signals from different sensors and turn it into an output such as activating a motor, turning LED on/off, connect to the cloud and many other actions. You can control your board functions by sending a set of instructions to the microcontroller on the board via Arduino IDE (referred to as uploading software). Unlike most previous programmable circuit boards, Arduino does not need an extra piece of hardware (called a programmer) in order to load a new code onto the board. You can simply use a USB cable. Additionally, the Arduino IDE uses a simplified version of C++, making it easier to learn to program. Finally, Arduino provides a standard form factor that breaks the functions of the micro-controller into a more accessible package. Board Types Various kinds of Arduino boards are available depending on different microcontrollers used. However, all Arduino boards have one thing in common: they are programed through the Arduino IDE. The differences are based on the number of inputs and outputs (the number of sensors, LEDs, and buttons you can use on a single board), speed, operating voltage, form factor etc. Some boards are designed to be embedded and have no programming interface (hardware), which you would need to buy separately. Some can run directly from a 3.7V battery, others need at least 5V. Here is a list of different Arduino boards available. Arduino boards based on ATMEGA328 microcontroller Board Name Operating Volt Clock Speed Digital i/o Analog Inputs PWM UART Programming Interface Arduino Uno R3 5V 16MHz 14 6 6 1 USB via ATMega16U2 Arduino Uno R3 SMD 5V 16MHz 14 6 6 1 USB via ATMega16U2 Red Board 5V 16MHz 14 6 6 1 USB via FTDI Arduino Pro 3.3v/8 MHz 3.3V 8MHz 14 6 6 1 FTDI-Compatible Header Arduino Pro 5V/16MHz 5V 16MHz 14 6 6 1 FTDI-Compatible Header Arduino mini 05 5V 16MHz 14 8 6 1 FTDI-Compatible Header Arduino Pro mini 3.3v/8mhz 3.3V 8MHz 14 8 6 1 FTDI-Compatible Header Arduino Pro mini 5v/16mhz 5V 16MHz 14 8 6 1 FTDI-Compatible Header Arduino Ethernet 5V 16MHz 14 6 6 1 FTDI-Compatible Header Arduino Fio 3.3V 8MHz 14 8 6 1 FTDI-Compatible Header LilyPad Arduino 328 main board 3.3V 8MHz 14 6 6 1 FTDI-Compatible Header LilyPad Arduino simple board 3.3V 8MHz 9 4 5 0 FTDI-Compatible Header Arduino boards based on ATMEGA32u4 microcontroller Board Name Operating Volt Clock Speed Digital i/o Analog Inputs PWM UART Programming Interface Arduino Leonardo 5V 16MHz 20 12 7 1 Native USB Pro micro 5V/16MHz 5V 16MHz 14 6 6 1 Native USB Pro micro 3.3V/8MHz 5V 16MHz 14 6 6 1 Native USB LilyPad Arduino USB 3.3V 8MHz 14 6 6 1 Native USB Arduino boards based on ATMEGA2560 microcontroller Board Name Operating Volt Clock Speed Digital i/o Analog Inputs PWM UART Programming Interface Arduino Mega 2560 R3 5V 16MHz 54 16 14 4 USB via ATMega16U2B Mega Pro 3.3V 3.3V 8MHz 54 16 14 4 FTDI-Compatible Header Mega Pro 5V 5V 16MHz 54 16 14 4 FTDI-Compatible Header Mega Pro Mini 3.3V 3.3V 8MHz 54 16 14 4 FTDI-Compatible Header Arduino boards based on AT91SAM3X8E microcontroller Board Name Operating Volt Clock Speed Digital i/o Analog Inputs PWM UART Programming Interface Arduino Mega 2560 R3 3.3V 84MHz 54 12 12 4 USB native Print Page Previous Next Advertisements ”;