Lecture 4: Basic Electronics Lecture 4 Page: 1 Brief Introduction to Electronics and the Arduino colintan@nus.edu.sg
Lecture 4: Basic Electronics Page: 2 Objectives of this Lecture By the end of today s lecture you will be able to: Understand the use of resistors and other passive components in circuit design. Understand how to use a breadboard. Understand the Arduino Mega.
Lecture 4: Basic Electronics Page: 3 Power Supply Power supply is divided into Vcc (or Vin) and GND. Vcc is the positive side of the supply, and on TTL (transistor-transistor logic) devices like the Arduino, is usually 5 volts. GND is ground, the negative side of the circuit.
Lecture 4: Basic Electronics Page: 4 Resistors A resister is a passive component (does not significantly alter the properties of a signal) that is used to reduce current. The symbol for a resistor is shown below:
Lecture 4: Basic Electronics Page: 5 Resistors Resistance is measured in ohms. You can read the value of a particular resistor by looking at the colored stripes on its body:
Lecture 4: Basic Electronics Page: 6 Resistors Example: We have a resistor with green, blue and brown stripes on its body: Green: 5 Blue: 6 Brown: 1 zero. Total -> 560 ohms Another resistor as brown, black and orange stripes. Brown: 1 Black 0 Orange: 3 zeros. This is a 10000 ohm resistor, or 10 kilohms.
Lecture 4: Basic Electronics Page: 7 Resistors Resistors are used for: Limiting current through a system. This is to prevent damage to sensitive devices like lightemitting diodes (LEDs) and transistors. Dividing voltages. This is used to get a lower voltage from a higher voltage, e.g. to reduce 5v to 3.3v. Also used in transistor amplifiers to bias the base to reduce noise. Pulling up or down inputs.
Lecture 4: Basic Electronics Page: 8 Resistors Current Limiting The LEDs you are given can take a maximum of 20mA. If you exceed this current by too much, your LED will smoke and you will owe me a new LED. Our Arduino General Purpose Input/Output (GPIO) pins produce 5v. From ohm s law, V=RI, or R=V/I R = 5 / 0.02 = 250 ohms.
Lecture 4: Basic Electronics Page: 9 Resistors Real resistors are offered in a small set of values (see later). The closest is 270 ohms Your Arduino kit only has 330 ohms (orange-orange-brown) and 10 kilohms. Our final circuit looks like this:
Lecture 4: Basic Electronics Page: 10 Resistors Voltage Divider A voltage divider is used to step down a voltage source. We will use a circuit like this: The voltage Vout is given by:
Lecture 4: Basic Electronics Page: 11 Resistors Voltage Divider: E.g. we want to reduce a source voltage from 5v to 3.3v (very important for low-power electronics) We choose R2 to be 1k. We have 3.3 = [1/(1+R1)] * 5 R1=0.515k. Closest value is 510 ohms Our final output voltage is 5 * [1/(1+0.51)] = 3.31v.
Lecture 4: Basic Electronics Page: 12 Resistors Voltage divider circuits are also used for sensing light, when combined with a light dependent resistor.
Lecture 4: Basic Electronics Page: 13 Resistors Suppose an LDR s resistance is 10 ohms in bright light and 50000 ohms in the dark (very typical values). Suppose R2 is fixed at 330 ohms, input voltage is 5v. The output voltage Vout in the light would be 5 * (330 / 340) = 4.85v The output voltage Vout in the dark would be 5 * (330/50330) =0.03v. The Analog-Digital Converters (ADC) on the Arduino are able to sense this difference in voltages.
Lecture 4: Basic Electronics Page: 14 Resistors Circuit inputs should never be left hanging. We must always weakly pull them up to 5V or drop them to 0V. We use pull-up and pull-down resistors for this. We always use relatively large resistors like 10k so that the current is low.
Lecture 4: Basic Electronics Page: 15 Resistors
Lecture 4: Basic Electronics Page: 16 Resistors Common Resistor Values The Arduino kit comes with 330 ohm and 10k ohm resistors.
Lecture 4: Basic Electronics Page: 17 Light Emitting Diodes Similar to small bulbs, but much more energy efficient. The LEDs you are given have a maximum safe current load of 20 ma. If you put more than 20 ma the magic smoke will escape (more on this later). Always use a current limiting resistor! Two legs: Longer leg -> Positive terminal. Shorter leg -> Connect to GND.
Lecture 4: Basic Electronics Page: 18 Potentiometer A potentiometer functions like an adjustable voltage divider. Turn the dial to adjust the output voltage. Has 3 legs: First and third leg connect to VCC and GND (Polarity not important). Second leg is the output voltage.
Lecture 4: Basic Electronics Page: 19 The Breadboard You are provided with a breadboard:
Lecture 4: Basic Electronics Page: 20 The Breadboard A breadboard s holes are internally connected in very particular ways:
Lecture 4: Basic Electronics Page: 21 The Breadboard This means that you must connect your components carefully to prevent short circuits:
Lecture 4: Basic Electronics Page: 22 The Breadboard
Lecture 4: Basic Electronics Page: 23 The Breadboard Connecting resistors correctly:
Lecture 4: Basic Electronics Page: 24 The Breadboard The top two rows are used for power supply. Conventionally the top row is for Vcc and bottom is for GND.
Lecture 4: Basic Electronics Page: 25 The Breadboard
Lecture 4: Basic Electronics Page: 26 Wire Color Codes You should ALWAYS color code your wires: Use BLACK wires for GND. Use RED wires for VCC. ALWAYS use the same colors for the same function. E.g.: Blue for input, brown for output. Yellow for TX, green for RX. Etc. Color-coding makes it easier to debug your circuit.
Lecture 4: Basic Electronics Page: 27 The Arduino Mega You are also issued with an Arduino Mega board:
Lecture 4: Basic Electronics Page: 28 The Arduino Mega The Arduino Mega is based on the Atmega 2560 microcontroller chip. 256KB of flash memory. 4KB of EEPROM. Good for storing data permanently. Will be damaged if written to too often. 8KB of RAM 16 MHz clock.
Lecture 4: Basic Electronics Page: 29 The Arduino Mega The pins labelled 1 to 53 are General Purpose Input/Output pins. These are 5v TTL pins. GPIO commands: pinmode Sets a pin to be input or output. digitalread Read the pin. You will get 1 if there is a 5V input to the pin, 0 if 0V. digitalwrite Write to the pin. If you write a HIGH, the pin will be driven to 5v. If you write a LOW, the pin will be driven to 0v.
Lecture 4: Basic Electronics Page: 30 The Arduino Mega
Lecture 4: Basic Electronics Page: 31 The Arduino Mega
Lecture 4: Basic Electronics Page: 32 Example #define inputpin 10 #define outputpin 13 void setup() { pinmode(10, INPUT); pinmode(13, OUTPUT); } void loop() { unsigned int flag; flag=digitalread(10); if(flag) digitalwrite(13, HIGH); else digitalwrite(13, LOW); } int main() { setup(); while(1) loop(); return 0; }
Lecture 4: Basic Electronics Page: 33 Handling Analog Data Digital input and output is simple: Either ON or OFF. Analog input and output is more complex: Temperature reading of 23.12C People of heights 1.62m, 1.76m, 1.83m, etc. Key issues with analog input: Infinite set of possible values. E.g. Current temperature is 24C, or 23.9C, or 23.91C, or 23.912C, or 23.9119C, or 23.91194C, or 23.911943C, or.
Lecture 4: Basic Electronics Page: 34 Handling Analog Data The pins labelled A0 to A15 are for analog inputs, pins 1 to 13 marked PWM are analog outputs.
Lecture 4: Basic Electronics Page: 35 Handling Analog Data
Lecture 4: Basic Electronics Page: 36 Power Supply The power supply pins supply 5 volts, 3.3 volts and GND. Vin is for supplying power to the Arduino. Maximum current draw of 200mA. Do not exceed this.
Lecture 4: Basic Electronics Page: 37 Analog Programming on Arduino #define inputpin 0 #define outputpin 5 // Remaps from 0-1023 to 0-255 int remap(int val) { return (int)(val * 255.0 / 1023.0); } int main() { while(1) { // Note: ADC returns values // from 0 to 1023, while // analogwrite accepts // values from 0 to 255. int val=analogread(inputpin); int outval=remap(val); analogwrite(outputpin, outval); } // while(1) return 0; }
Lecture 4: Basic Electronics Page: 38 Serial Programming There are four serial interfaces on the Mega, marked TX, RX, TX1, RX1, TX2, RX2, TX3, RX3.
Lecture 4: Basic Electronics Page: 39 Serial Programming Serial ports are accessed using the Serial, Serial1, Serial2, Serial3 static objects. Serial.begin(baudrate) Serial.write(ch) ch = Serial.read(); String str=serial.readstring() Serial.available() Serial.print/Serial.println Serial.setTimeout(timeoutInMS)
Lecture 4: Basic Electronics Page: 40 Serial Programming