background preloader

Secrets of Arduino PWM

Secrets of Arduino PWM
Pulse-width modulation (PWM) can be implemented on the Arduino in several ways. This article explains simple PWM techniques, as well as how to use the PWM registers directly for more control over the duty cycle and frequency. This article focuses on the Arduino Diecimila and Duemilanove models, which use the ATmega168 or ATmega328. If you're unfamiliar with Pulse Width Modulation, see the tutorial. PWM has several uses: Dimming an LED Providing an analog output; if the digital output is filtered, it will provide an analog voltage between 0% and 100% . Simple Pulse Width Modulation with analogWrite The Arduino's programming language makes PWM easy to use; simply call analogWrite(pin, dutyCycle), where dutyCycle is a value from 0 to 255, and pin is one of the PWM pins (3, 5, 6, 9, 10, or 11). Probably 99% of the readers can stop here, and just use analogWrite, but there are other options that provide more flexibility. Bit-banging Pulse Width Modulation Using the ATmega PWM registers directly Related:  Voice Synthesis in Arduino

How to Configure Arduino Timer 2 Registers to Drive an Ultrasonic Transducer with a Square Wave | Fiz-ix The Arduino IDE has many built-in commands to produce PWM outputs but directly setting the timer registers gives you much more flexibility and power. Below I show how to configure the 8-bit Timer/Counter2 on the ATmega328 (Ardunio UNO) to generate a 40 kHz square wave on Arduino digital pin 11. Why 40 kHz? I want to use it to drive a 40 kHz ultrasonic transducer for a project I am working on. void startTransducer() { TCCR2A = _BV(COM2A0) | _BV(WGM21) | _BV(WGM20); TCCR2B = _BV(WGM22) | _BV(CS20); OCR2A = B11000111; // 199, so timer2 counts from 0 to 199 (200 cycles at 16 MHz) } void setup() { pinMode(11, OUTPUT); startTransducer(); } void loop() { } The _BV(XXX) function sets the XXX bit of whatever register you are working with to one. #define _BV(bit) (1 << (bit)) The TCCR2A and TCCR2B 8-bit registers have the following structures: TCCR2A - [COM2A1, COM2A0, COM2B1, COM2B0, reserved, reserved, WGM21, WGM20] TCCR2B - [FOC2A, FOC2B, reserved, reserved, WGM22, CS22, CS21, CS20]

CapacitiveSensor by Paul Badger Download Download This updated CapacitiveSensor version 05 adds support for Arduino Due and other non-AVR boards. Overview The capacitiveSensor library turns two or more Arduino pins into a capacitive sensor, which can sense the electrical capacitance of the human body. Version 04 adds support for Arduino 1.0, and fixes an obscure possible race condition with Tone, Servo and other libraries that perform I/O in interrupt context. Version 03 has been updated to C++ and supports multiple inputs. Applications Capacitive sensing may be used in any place where low to no force human touch sensing is desirable. A capacitive sensor covered with paper or other insulator also acts as fairly good (human touch) pressure sensor with an approximately logarithmic response. How it works The capacitiveSensor method toggles a microcontroller send pin to a new state and then waits for the receive pin to change to the same state as the send pin. Library Methods Resistor Choice

Simple Arduino audio samples This tutorial explains how to do simple playback of short (~4 second), low-bitrate (8 KHz) audio samples from Arduino using only a speaker. It’s based on the PCMAudio code by Michael Smith. Pre-Requisites You’ll need: An Arduino Uno or Duemilanove A speaker with wires soldered to it. Explanation The audio playback works using two of the Arduino board’s timers, hardware functionality of the AVR (ATmega328) microcontroller that’s normally used to generate PWM output with the analogWrite() function. Download and Install the PCM Library for Arduino Download: damellis-PCM- something .zip Install: Unzip the file. Upload the Example Launch the Arduino software. Library Functions The library only has two functions: startPlayback() and stopPlayback(). The stopPlayback() function doesn’t take any arguments and will stop playback of the current sample. Note that the example also uses a couple of unusual Arduino constructions. Encode Your Own Audio Sample Download: Unzip and run the application.

TinyGPS | Arduiniana A Compact Arduino GPS/NMEA Parser TinyGPS is designed to provide most of the NMEA GPS functionality I imagine an Arduino user would want – position, date, time, altitude, speed and course – without the large size that seems to accompany similar bodies of code. To keep resource consumption low, the library avoids any mandatory floating point dependency and ignores all but a few key GPS fields. Usage To use, simply create an instance of an object like this: Feed the object serial NMEA data one character at a time using the encode() method. You can then query the object to get various tidbits of data. Statistics The stats method provides a clue whether you are getting good data or not. chars – the number of characters fed to the objectsentences – the number of valid $GPGGA and $GPRMC sentences processedfailed_checksum – the number of sentences that failed the checksum test Integral values Values returned by the core TinyGPS methods are integral. Using Floating Point Date/time cracking Download

PWM - an overview PWM sometimes seems to be a misunderstood and complicated topic. This tutorial will cover the basics of what PWM is, what it can be used for, and how to use the AVR controllers to generate PWM. PWM stands for Pulse Width Modulation. The digital world Since microcontrollers live in a digital world then their output pins can be either low (0v) or high (5v). AVR microcontrollers have Analogue To Digitals Convertors (ADC) to convert a voltage from the analogue world to a number but do not have Digital to Analogue Convertors (DAC) to convert digital numbers back into variable voltages. PWM is the closest solution. By turning an output pin repeatedly high and low very quickly then the result is an average of the amount of time the output is high. Why does this work? For example: if you connect a motor to a battery then it will, eventually, rotate at full speed. Servos are another example. How do we create a PWM signal Microcontrollers are very good with whole (integer) numbers. Frequency Duty Cycle

NewSoftSerial | Arduiniana A New Software Serial Library for Arduino News: NewSoftSerial is in the core! Starting with Arduino 1.0 (December, 2011), NewSoftSerial has replaced the old SoftwareSerial library as the officially supported software serial library. NewSoftSerial is the latest of three Arduino libraries providing “soft” serial port support. Without interrupts, your program’s design is considerably restricted, as it must continually poll the serial port at very short, regular intervals. Improvements NewSoftSerial offers a number of improvements over SoftwareSerial: It inherits from built-in class Print, eliminating some 4-600 bytes of duplicate codeIt implements circular buffering scheme to make RX processing more efficientIt extends support to all Arduino pins 0-19 (0-21 on Arduino Mini), not just 0-13It supports multiple simultaneous soft serial devices. *But see below for an important caveat on multiple instances. Using Multiple Instances What does this mean, exactly? Signal Inversion Library Version

Frequency | Aquaticus General rule for choosing frequency for PWM signal: higher is better. Use as high frequency as you can. This rule is not valid only if you need low frequency wave e.g. for blinking LED. Maximum frequency is often limited by the external components, basically for elements like MOSFETs number of switches in 1 second is limited. For example, if you use popular L298 to control motor speed, you can use up to 40kHz. Timers can be clocked directly by the system clock or by the prescaler. For ATmega16/32 timer0 and timer1 share the same prescaler module and can divide frequency to: F/1, F/8, F/64, F/256 or F/1024, where F is system clock frequency. If prescaler divider is 1, timer is clocked directly by the system clock. Frequency in this mode can be calculated by the following equation: Here is the table of PWM frequency for 1MHz, 8MHz and 16Mhz (timer1 prescaler values shown). Here are the table of PWM frequency for 1MHz, 8MHz and 16MHz (timer1 prescaler values shown).

High Performance Line Follower Robot: 12 Steps AdWords We use AdWords to deploy digital advertising on sites supported by AdWords. Ads are based on both AdWords data and behavioral data that we collect while you’re on our sites. The data we collect may include pages you’ve visited, trials you’ve initiated, videos you’ve played, purchases you’ve made, and your IP address or device ID. LiveRamp We use LiveRamp to deploy digital advertising on sites supported by LiveRamp. Doubleclick We use Doubleclick to deploy digital advertising on sites supported by Doubleclick. RocketFuel We use RocketFuel to deploy digital advertising on sites supported by RocketFuel. Twitter We use Twitter to deploy digital advertising on sites supported by Twitter. Facebook We use Facebook to deploy digital advertising on sites supported by Facebook. Sprinklr We use Sprinklr to deploy digital advertising on sites supported by Sprinklr. Dstllery We use Dstllery to deploy digital advertising on sites supported by Dstllery. Marin LinkedIn Demandbase Yandex AdForm Baidu Yahoo!