Arduino: Difference between revisions
Line 487: | Line 487: | ||
* Part: https://www.sparkfun.com/products/9806 | * Part: https://www.sparkfun.com/products/9806 | ||
* Example: SIK guide project 2 (use pot to control the blink rate of LED) from [https://www.sparkfun.com/products/retired/11227 Sparkfun Inventor Kit]. | * Example: SIK guide project 2 (use pot to control the blink rate of LED) from [https://www.sparkfun.com/products/retired/11227 Sparkfun Inventor Kit]. | ||
* On potentiometer, number 1 is 5v and number 3 is 0v. | * On potentiometer, number 1 is 5v and number 3 is 0v although we can swap these 2 pins to get reverse effect when using the knob. | ||
* A copy of the SIK guide and example code are available at my [https://github.com/arraytools/SIK github] page. | * A copy of the SIK guide and example code are available at my [https://github.com/arraytools/SIK github] page. | ||
Revision as of 19:36, 21 July 2014
Hardware
Uno
The Arduino Uno is a microcontroller board based on the ATmega328 (datasheet). It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz ceramic resonator, a USB connection, a power jack, an ICSP header, and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started.
This means that the Uno can execute 16 million 8 bit instructions per second.
Due
The Arduino Due is a microcontroller board based on the Atmel SAM3X8E ARM Cortex-M3 CPU (datasheet). It is the first Arduino board based on a 32-bit ARM core microcontroller. It has 54 digital input/output pins (of which 12 can be used as PWM outputs), 12 analog inputs, 4 UARTs (hardware serial ports), a 84 MHz clock, an USB OTG capable connection, 2 DAC (digital to analog), 2 TWI, a power jack, an SPI header, a JTAG header, a reset button and an erase button.
Since UNO is 8-bit and DUE is 32-bit, there is difference in terms of storage. On the Arduino Uno (and other ATMega based boards) an int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1).
On the Arduino Due, an int stores a 32-bit (4-byte) value. This yields a range of -2,147,483,648 to 2,147,483,647 (minimum value of -2^31 and a maximum value of (2^31) - 1). See http://arduino.cc/en/Reference/int and Discussion of comparing UNO & Due.
Another interesting point is neither of them have floating point support (Beaglebone bone and Rasp Pi do have FP).
Comparison of Boards
- https://www.sparkfun.com/arduino_guide
- http://arduino.cc/en/Products.Compare
- http://learn.adafruit.com/adafruit-arduino-selection-guide/arduino-comparison-chart
- http://www.tested.com/tech/robots/456466-know-your-arduino-guide-most-common-boards/
Memory
- http://playground.arduino.cc/Learning/Memory
- http://learn.adafruit.com/memories-of-an-arduino/arduino-memories
- Flash memory (program space), is where the Arduino sketch is stored. Flash memory is the same technology used for thumb-drives and SD cards. It is non-volatile, so your program will still be there when the system is powered off.
- SRAM (static random access memory) is where the sketch creates and manipulates variables when it runs. This memory includes 3 kinds of space: static data, heap and stack.
- EEPROM is memory space that programmers can use to store long-term information.
Arduino IDE
The serial port is ttyACM0 under my Ubuntu. To detect the serial port device, first run
ls -t /dev
Then plug in Arduino UNO and run the above command again. See playground.arduino.cc.
Source code in Github
https://github.com/arduino/Arduino/tree/master/hardware/arduino/cores/arduino
Install Arduino IDE under Ubuntu
- http://askubuntu.com/questions/107619/how-do-i-install-the-arduino-ide
- http://blog.markloiseau.com/2012/05/install-arduino-ubuntu/
Using source code (For some reason, I cannot find ~/.arduino/preference.txt file with this approach)
sudo apt-get install arduino-core sudo apt-get install openjdk-7-jre # Go to http://arduino.cc/en/main/software to download the latest software tar xzvf arduino-1.0.5-linux32.tgz cd arduino-1.0.5 sudo ./arduino
The 2nd approach is (I think it is better since I can find and modify ~/.arduino/preference.txt file).
sudo apt-get update && sudo apt-get install arduino arduino-core
After Arduino IDE is installed, I can change the font (Line 11 of preference.txt file) from Monospaced DejaVu Sans Mono,plain,12 to something like
editor.font=DejaVu Sans Mono,bold,16 OR editor.font=Courier New,bold,16
PS. Open AbiWord to check available fonts in Ubuntu OS.
Arduino Reset Button
To "reset" the Uno power it down, hold down the reset button, and, while still holding the reset button down, power it up again. This will prevent the existing sketch from running. You can then upload a new sketch.
C users
- http://www.javiervalcarce.eu/wiki/Program_Arduino_with_AVR-GCC
- http://code.google.com/p/libarduino/
- http://www.joakimlinde.se/microcontrollers/arduino/avr/
Programming using Scratch
Since this will involve uploading a firmware to Arduino, we now can only do programming using S4A. We can go back to use Arduino IDE for programming if we want to discard using S4A.
s4a is available as deb package.
Simple example (button & LED)
- Connect wires according to http://s4a.cat/. I use 330 Ohm resistor. The digital pin I use is 3, not 2.
- sudo s4a to launch S4A
- Follow the instruction in instructables.com to try 1. LED blinking and 2. LED and button in S4A.
One problem with this idea is the arduino board needs to be connected to computer all the time.
Tutorials
- http://www.nunoalves.com/classes/spring_2012_cpe355/
- Arduino Tip, Tricks and Techniques
- Serial monitor
- http://tronixstuff.wordpress.com/tutorials/
- http://startingelectronics.com/beginners/
- https://www.sparkfun.com/tutorials/57
- http://www.ladyada.net/learn/arduino/
- Stepper motor See the example 1 where the motor turns in two directions and example 2 code where the motor turns continuously in one direction. The connection of motor to the arduino is very simple: Just 1. connecting IN1, IN2, IN3, IN4 to pin 8,9,10,11 and 2. connecting GND to GND in arduino and VCC to 5v in arduino. The connection of 28BYJ148 motor to uln2003 is obvious. See my video on Youtube.
- Micro servo. See http://magnusglad.wordpress.com/2013/03/13/tested-my-sg90-servo-today/ for an instruction and my video on Youtube.
- Check google to see the difference between dc motor and servo. This site explains the difference among dc motor, servo motor and step motor. Adafruit.com has an article to explain stepper motors.
- Adafruit motor shield v2. It can be used to control DC motors, stepper motors and servos. It can drive up to 4 DC motors or 2 stepper motors.
Arduino IDE, Books, Forums
IDE
- Currently I am using version 1.0.4
- On ubuntu, we need to run arduino using sudo; otherwise, the serial port is greyed out.
- Save/Save As will create a folder and a file with the extension .ino.
- Cheat sheet https://dlnmh9ip6v2uc.cloudfront.net/learn/materials/8/Arduino_Cheat_Sheet.pdf
Books
- Getting Started with Arduino by Massimo Banzi.
- Practical Arduino by Jon Oxer and Hugh Blemings.
- Exploring Arduino: Tools and Techniques for Engineering Wizardry and Book's website.
- Arduino Workshop: A Hands-On Introduction with 65 Projects
Forums
- (Blog) Make and Instructables.
- Freeduino
Processing/Graph
It is possible to send a byte of data from the Arduino to a personal computer and graph the result. This is called serial communication because the connection appears to both the Arduino and the computer as a serial port, even though it may actually use a USB cable.
- First, download and install Processing software (currently 2.0.3)
- Check out http://arduino.cc/en/Tutorial/Graph for wiring. Very simple. Just one potentiometer and 3 wires (one goes to gnd, 2nd one goes to 5v and the 3rd one connects to A0).
- Check out http://www.dustynrobots.com/news/seeing-sensors-how-to-visualize-and-save-arduino-sensed-data/ for the idea of how to plot the analog data using Processing. It contains 2 steps: 1. Open Arduino IDE (sudo is necessary if OS is not Windows) and compile/run a code to print byte to serial using Serial.write(). 2. Open Processing (sudo is necessary if OS is not Windows) and run a code. We shall be able to see a beautiful streaming graph appearing on screen. Both codes are attached below.
Arduino
int sensorPin = A0; // analog input pin to hook the sensor to int sensorValue = 0; // variable to store the value coming from the sensor void setup() { Serial.begin(9600); // initialize serial communications } void loop() { sensorValue = analogRead(sensorPin)/4; // read the value from the sensor Serial.write(sensorValue); // print bytes to serial delay(10); }
Processing
import processing.serial.*; Serial myPort; // The serial port float xPos = 0; // horizontal position of the graph void setup () { size(800, 600); // window size // List all the available serial ports println(Serial.list()); String portName = Serial.list()[0]; myPort = new Serial(this, portName, 9600); background(#EDA430); } void draw () { // nothing happens in draw. It all happens in SerialEvent() } void serialEvent (Serial myPort) { // get the byte: int inByte = myPort.read(); // print it: println(inByte); float yPos = height - inByte; // draw the line in a pretty color: stroke(#A8D9A7); line(xPos, height, xPos, height - inByte); // at the edge of the screen, go back to the beginning: if (xPos >= width) { xPos = 0; // clear the screen by resetting the background: background(#081640); } else { // increment the horizontal position for the next reading: xPos++; } }
More:
Robot based on BOE shield for Arduino
My robot is based on 'Shield for Arduino' which means I can use Arduino IDE + C code style to write my code instead of using PBASIC programming which is used in another similar robot.
Projects from each chapters
Chapter 3. Activity 2: Re-test the servos
We can use Pins 11 and 12 instead of 12 and 13. Be sure to adjust your code accordingly.
Chapter 4. Activity 2
First time we disconnect robot from the computer and let it go (forward, left turn, right turn and back ward).
Chapter 5 (Whiskers). Activity 3 or 4
See my robot is in action on http://www.youtube.com/watch?v=sjzQI6GHe3w.
Chapter 7 (Infrared headlight). Activity 4
Similar to Chapter 5 but IR detectors are used. See the robot in action on http://www.youtube.com/watch?v=K5pIZ5XcP7Y.
Activity 7: Drop-off detection (Very Cool).
This site contains some more information to use infrared LED to avoid object by testing on arduino board.
Using regular IR remote controll
The Arduino code provided for this project makes the BOE Shield-Bot recognize Sony IR remote signals only! For example, the BRIGHTSTAR universal remote sold by Parallax is programmed by holding the SET-UP button down until an LED on the remote lights up, then entering the code 605. Different models of remotes may use different codes and configuration methods. Check your remote's docs! See the instruction and arduino code in IR Remote Controlled Shield-Bot.
Using ping))) with BOEShield
Information and arduino code from Parallax. I find the code by searching within parallax.com. See http://thoughtfix.com/blog/2012/3/25/arduino-boe-shield-ping-and-a-servo.html for arduino code. Some modified code here. My recorded video is available on Youtube.
Other robots
- (Tutorial) http://www.foxytronics.com/learn/robots/how-to-make-your-first-arduino-robot
- http://www.robotshop.com/
- http://www.ez-robot.com/ including EZ-B Bluetooth Servo Tamiya Bulldozer Robot
- Arduino controlled Bluetooth-bot
- (Book) Arduino Robotics By John-David Warren , Josh Adams , Harald Molle
Video
The astounding athletic power of quadcopters from TED.
Kit
Projects
My own list of top 10
Simple LED
GND --- wire --- 300 Ohm --- short end of LED --- long end of LED --- wire --- 5v/3.3v.
The 300 Ohm resistor can be replaced by anyone between 100 Ohm and 10K Ohm.
7 Segment LED
- Design. Note that for common anode, we need to connect pin 3, 8 to 5V and for common cathode we want to connect pin 3,8 to gnd.
- Arduino example (digits). I need to change seven_seg_digit to 1-seven_seg_digit in digitWrite() function since mine has common anode.
- How to represent all alphabet
LCD Keypad shield
This Arduino shield includes a 16x2 HD44780 White on Blue LCD module and a 5 push button keypad for menu selection and user interface programming. The shield can be directly used by opening Arduino > Example > CrystalLCD > Autoscroll as long as we have changed the pin numbers (see comments from dx.com).
- (Wiki) http://www.dfrobot.com/wiki/index.php/Arduino_LCD_KeyPad_Shield_%28SKU:_DFR0009%29
- (Display buttons/LCD Keypad) http://arduinotronics.blogspot.com/2012/06/lcd-keypad-shield.html. The source code is in github.
- (Display barchart) http://www.mathias-wilhelm.de/arduino/shields/dfrobot-lcd-16x2-shield/ I saved the source code in my github.
- (Buy from) http://dx.com/p/lcd-keypad-shield-for-arduino-duemilanove-lcd-1602-118059#.UtyCQNdOnoo
Control DC motors
- Magician Chassis
- DC motor's direction using an H-bridge
- PC fan motor (brushless): using TIP120 transistor to control motors and high power devices.
- Reading pc fan rpm. The pc fan can be just powered by a 9V battery. I save a copy of the code in github. PS. 9 wires and 3 crocodile clips in addition to 1 PC case fan are needed.
Using Web to read/write GPIO (it works)
http://uggettif.blogspot.com/2012/11/controlling-arduino-from-web-page.html
Using Node/Websocket to create a Web page and control Arduino
- http://danieldvork.in/arduino-controlled-html5-etch-a-sketch-using-node-js-and-websockets/
- http://semu.github.io/noduino/
- http://www.olivierklaver.com/chapter_3.html
Test Ethernet Shield
Use sketch_12_01_dhcp from Programming Arduino Next Steps. Or
// sketch_12_01_dhcp #include <SPI.h> #include <Ethernet.h> byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 }; void setup() { Serial.begin(9600); while (!Serial){}; if (Ethernet.begin(mac)) { Serial.println(Ethernet.localIP()); } else { Serial.println("Could not connect to network"); } } void loop() { }
Open Serial Monitor. Mine shows 10.42.0.37 when I use ethernet 1 from PC to share the internet connection.
Arduino with bluetooth
- RFduino and Company web site here. App is running on iphone.
- Google: arduino bluetooth android and an example by using JY-MCU Bluetooth module.
- (not bluetooth) Xbee wireless http://www.youtube.com/watch?v=WTnC1bHoaDM. Information from Parallax.
- Bluefruit EZ-Link from Adafruit.
Accelerometers & Gyroscopes
- http://www.robotshop.com/blog/en/arduino-5-minute-tutorials-lesson-7-accelerometers-gyros-imus-3634
- http://www.youtube.com/watch?v=HYUYbN2gRuQ
- http://arduino.cc/en/Tutorial/ADXL3xx
- http://www.instructables.com/id/Use-an-Accelerometer-and-Gyroscope-with-Arduino/
- http://www.instructables.com/id/Guide-to-gyro-and-accelerometer-with-Arduino-inclu/ (good demonstration)
- http://dx.com/p/adxl345-digital-3-axis-gravity-acceleration-sensor-module-blue-149476
- https://www.sparkfun.com/tutorials/240 (from google: ADXL345 Arduino)
- http://learn.adafruit.com/adxl345-digital-accelerometer/programming Calibration (ADXL345)
- Bump detection especially in robot http://letsmakerobots.com/node/33979 (MMA7361)
- http://www.freetronics.com/pages/am3x-quickstart-guide#.Uk4clteVv0o (Understand output and calibration)
- http://www.vetco.net/catalog/product_info.php?products_id=12790 (MMA7361) mentions applications for this device on your Arduino include navigation, motion sensing, speed/movement data logging, etc. Hey, you could make an Arduino-based pedometer.
- http://ardadv.blogspot.com/2012/03/its-just-jump-to-left-part-ii.html
Python and Arduino
Monitor a movement (serial port) and send an email
http://learn.adafruit.com/arduino-lesson-17-email-sending-movement-detector/python-code
The arduino program is launched first. The arduino is used to send messages and python is used to read messages. So arduino program can be run without a Python program.
Monitor files and send signals to LED
Write one arduino and one python file http://www.joakimlinde.se/microcontrollers/arduino/ or http://www.olgapanades.com/blog/controlling-arduino-with-python/
See also arduino.cc website for mire info about Python talking to Arduino via a serial interface.
Electronics
Clock cycles
The following code was used to measure the time spent on running 2 statements: one is delay(1000) and the other is Serial.println(). I got an output of 8 (most of time) or 4 (few times) microseconds. The code was modified from Arduino Reference, this discussion or another discussion.
Recall that nano second=10^(-9) second and micro second = 10^(-6) second. On 16 MHz Arduino boards (e.g. Duemilanove and Nano), this function has a resolution of four microseconds (i.e. the value returned is always a multiple of four).
unsigned long time; void setup(){ Serial.begin(9600); } void loop(){ Serial.print("Time: "); time = micros(); delay(1000); // wait a second so as not to send massive amounts of data // We can also move this line to the end of loop() function to see the difference Serial.println(micros() - time); }
If loop is empty, it will run at 16 MHz. If toggling a PIN is only line inside loop, it will run 16x10^6/2 loops since toggling a PIN took 2 clock cycles.
On a slightly more complex example,
unsigned long time; void setup(){ Serial.begin(9600); } void loop(){ Serial.print("Time: "); time = micros(); int angle = 0; while(angle < 180) Serial.println(sin(angle++/57.295)); Serial.println(micros() - time); // wait a second so as not to send massive amounts of data delay(1000); }
it took 1061804 microseconds on my Arduino UNO; that is 16*10^6/10^6 * 1061804/ instructions to execute. Here, 16*10^6 number denotes 16 MHz of the CPU and 10^6 denotes the number of micro seconds. In other words, there are billions of instructions were executed in 4 lines.
Instruction <> Cycle
http://forum.arduino.cc/index.php/topic,4324.0.html
Arduino CPU monitor
See this article from www.linuxuser.co.uk. We get real-time CPU data and display it with a series of LEDs. We’ll also look into adjusting the brightness of those LEDs with a potentiometer, and running the app as a background process with Forever.js.
Calculate resistor value for LED
To figure out which resistor to use I used the formula: R = (V1 - V2) / I
where: V1 = power supply voltage, V2 = LED voltage, I = LED current (usually 20mA which is .02A).
For example, my LED is 1.7V, it takes 20mA (which is .02 A) of current and my supply is 4.5V. So the math is...
R = (4.5V - 1.7V) / .02 A = 140 ohms
Also putting LEDs in series would add voltages together while in parallel won't add voltages but draw more current. That is if we put LEDs together, we need to multiply the current that one LED draws by the total number of LEDs I was using.
If we putting resistors in series, it would add ohms. But we put resistors in parallel, it would reduce resistance.
Timer
http://www.nunoalves.com/classes/spring_2012_cpe355/cpe355-02-f.pdf
Battery
Ah (Amp Hour)
http://overlandresource.com/what-is-an-amp-hour-and-how-to-calculate-battery-capacity
Interrupt
http://www.nunoalves.com/classes/spring_2012_cpe355/cpe355-02-b.pdf
See Chapter 3 of Programming Arduino Next Steps. On Arduino UNO, there are 2 interrupts (PIN 2 and 3). A simple sketch to test interrupts (using attachInterrupt function) is
// sketch 03_01_interrupts int ledPin = 13; void setup() { pinMode(ledPin, OUTPUT); attachInterrupt(0, stuffHapenned, FALLING; } void loop() { } void stuffHapenned() { digitalWrite(ledPin, HIGH); }
Note that PIN 13 has an LED (also know "L" LED since there is a label "L" next to it) connected on UNO board so we don't need to manually connect another LED. See Arduino.cc website.
PWM/Analog Output
See Chapter 1 of Programming Arduino Next Steps - Analog output. It also teaches how to use multimeter to measure analog output <Sketch_01_07_pwm>. It is noted that UNO does not have true analog output (Arduino Due has). On Arduino UNO board, the pins marked with little "~" (pins 3,5,6,9,10 and 11) can be used as analog outputs (with the analogWrite function).
See Chapter 3 of Programming Arduino Next Steps <Sketch_03_03_1kHz>.
Memory
I2C/TWI
SPI
UART
USB Programming
Network Programming
Digital Signal Processing
Writing Library
Relay module shield
- http://www.aliexpress.com/store/product/Four-relay-module4-relay-module-expansion-5V-12V/320981_598692534.html?tracelog=back_to_detail_b
- http://www.glacialwanderer.com/hobbyrobotics/?p=9
- https://www.sparkfun.com/products/10684
- Tutorial http://www.sainsmart.com/8-channel-dc-5v-relay-module-for-arduino-pic-arm-dsp-avr-msp430-ttl-logic.html
- Search amazon for relay module shield. The sellers tend to give connection instructions.
Transistor
https://learn.sparkfun.com/tutorials/transistors
potentiometer
- Part: https://www.sparkfun.com/products/9806
- Example: SIK guide project 2 (use pot to control the blink rate of LED) from Sparkfun Inventor Kit.
- On potentiometer, number 1 is 5v and number 3 is 0v although we can swap these 2 pins to get reverse effect when using the knob.
- A copy of the SIK guide and example code are available at my github page.
Embedded System
Introduction to Embedded Systems: Using ANSI C and the Arduino Development Environment by David Russell (Author), Mitchell Thornton (Series Editor)
Seller, Community
- http://arduino.cc/en/Main/Buy
- Arduino.cc forum
- adafruit and Adafruit forum
- Sparkfun
- Parallax Resources and Forum
- Element14 Community
- Newark
The CanaKit's Sparkfun inventor's kit for Arduino (SX10173) contains lots of stuff like 1N4148 diode, P2N222AG NPN transistor (youtube, potentiometer, 5V relay, temperature sensor, 74HC595 shift register, hobby servo and photo resistor/photocell. Circ-03 (or Circ-12 on the new guide) teaches how to use P2N222AG transistor and 1N4001 Diode to spin a motor. See the SIK guide in pdf from the above link. The resistors it has included have 330 ohm, 10K. I also purchased 3.3M resistors for another application.
In the Arduino budget pack, it includes 100 ohm, 1k and 10k resistors and photocell.
http://lifehacker.com/how-can-i-get-into-a-new-hobby-without-breaking-the-ban-489189582