Showing posts with label AVR Arduino Project. Show all posts
Showing posts with label AVR Arduino Project. Show all posts

Analog In, Out Serial using Arduino -c


Analog In, Out Serial

This example shows how to read an analog input pin, map the result to a range from 0 to 255, and then use that result to set the pulsewidth modulation (PWM) of an output pin to dim or brighten an LED.
Hardware Required
·         Arduino Board
·         Potentiometer
·         LED
·         220 ohm resistor

Circuit


Connect one pin from your pot to 5V, the center pin to analog pin 0, and the remaining pin to ground. Next, connect a 220 ohm current limiting resistor to digital pin 9, with an LED in series. The long, positive leg (the anode) of the LED should be connected to the output from the resistor, with the shorter, negative leg (the cathode) connected to ground.

Schematic


Code

In the program below, after declaring two pin assignments (analog 0 for your potentiometer and digital 9 for your LED) and two variables, sensorValue and outputValue, the only thing that you do will in the setup function is to begin serial communication.
Next, in the main loop of the code, sensorValue is assigned to store the raw analog value coming in from the potentiometer. Because the Arduino has an analogRead resolution of 0-1023, and an analogWrite resolution of only 0-255, this raw data from the potentiometer needs to be scaled before using it to dim the LED.
In order to scale this value, use a function called map()
outputValue = map(sensorValue, 0, 1023, 0, 255);

outputValue is assigned to equal the scaled value from the potentiometer. map() accepts five arguments: The value to be mapped, the low range and high range of the raw data, and the low and high values for that data to be scaled too. In this case, the sensor data is mapped down from its original range of 0 to 1023 to 0 to 255.
The newly mapped sensor data is then output to the analogOutPin dimming or brightening the LED as the potentiometer is turned. Finally, both the raw and scaled sensor values are sent to the Arduino serial window in a steady stream of data.
/*
  Analog input, analog output, serial output
 
 Reads an analog input pin, maps the result to a range from 0 to 255
 and uses the result to set the pulsewidth modulation (PWM) of an output pin.
 Also prints the results to the serial monitor.
 
 The circuit:
 * potentiometer connected to analog pin 0.
   Center pin of the potentiometer goes to the analog pin.
   side pins of the potentiometer go to +5V and ground
 * LED connected from digital pin 9 to ground
 
 created 29 Dec. 2008
 Modified 4 Sep 2010
 by Tom Igoe
 
 This example code is in the public domain.
 
 */

// These constants won't change.  They're used to give names
// to the pins used:
const 
int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const 
int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
void setup() {
  
// initialize serial communications at 9600 bps:
  
Serial.begin(9600); 
}
void loop() {
  
// read the analog in value:
  sensorValue 
= analogRead(analogInPin);          
  
// map it to the range of the analog out:
  outputValue 
= map(sensorValue, 0, 1023, 0, 255); 
  
// change the analog out value:
  
analogWrite(analogOutPin, outputValue);           

  
// print the results to the serial monitor:
  
Serial.print("sensor = " );                       
  
Serial.print(sensorValue);    
  
Serial.print("\t output = ");    
  
Serial.println(outputValue);   

  
// wait 10 milliseconds before the next loop
  
// for the analog-to-digital converter to settle
  
// after the last reading:
  
delay(10);                     
}

Controlling a Digital Potentiometer Using SPI


In this tutorial you will learn how to control the AD5206 digital potentiometer using Serial Peripheral Interface (SPI). Digital potentiometers are useful when you need to vary the resistance in a circuit electronically rather than by hand. Example applications include LED dimming, audio signal conditioning and tone generation.
In this example we will use a six channel digital potentiometer to control the brightness of six LEDs. The steps we will cover for implementing SPI communication can be modified for use with most other SPI devices.

Hardware Required

·         AD5206 Digital Potentiometer
·         Arduino Microcontroller Module
·         6 Light Emitting Diodes (LEDs)
·         6 220 ohm resistors
·         Hookup Wire

Introduction to the AD5206 Digital Potentiometer


The AD5206 is a 6 channel digital potentiometer. This means it has six variable resistors (potentiometers) built in for individual electronic control. There are three pins on the chip for each of the six internal variable resistors, and they can be interfaced with just as you would use a mechanical potentiometer. The individual variable resistor pins are labeled Ax, Bx and Wx, ie. A1, B1 and W1. For example, in this tutorial we will be using each variable resistor as a voltage divider by pulling one side pin (pin B) high, pulling another side pin (pin A) low and taking the variable voltage output of the center pin (Wiper). In this case, the AD5206 provides a maximum resistance of 10K ohms, delivered in 255 steps (255 being the max, 0 being the least).

Circuit


Schematic


Code

/*
  Digital Pot Control
  
  This example controls an Analog Devices AD5206 digital potentiometer.
  The AD5206 has 6 potentiometer channels. Each channel's pins are labeled
  A - connect this to voltage
  W - this is the pot's wiper, which changes when you set it
  B - connect this to ground.
 
 The AD5206 is SPI-compatible,and to command it, you send two bytes, 
 one with the channel number (0 - 5) and one with the resistance value for the
 channel (0 - 255).  
 
 The circuit:
  * All A pins  of AD5206 connected to +5V
  * All B pins of AD5206 connected to ground
  * An LED and a 220-ohm resisor in series connected from each W pin to ground
  * CS - to digital pin 10  (SS pin)
  * SDI - to digital pin 11 (MOSI pin)
  * CLK - to digital pin 13 (SCK pin)
 
 created 10 Aug 2010 
 by Tom Igoe
 
 Thanks to Heather Dewey-Hagborg for the original tutorial, 2005
 
*/


// inslude the SPI library:
#include <SPI.h>

// set pin 10 as the slave select for the digital pot:
const 
int slaveSelectPin = 10;
void setup() {
  
// set the slaveSelectPin as an output:
  
pinMode (slaveSelectPin, OUTPUT);
  
// initialize SPI:
  SPI.
begin(); 
}
void loop() {
  
// go through the six channels of the digital pot:
  
for (int channel = 0; channel < 6; channel++) { 
    
// change the resistance on this channel from min to max:
    
for (int level = 0; level < 255; level++) {
      digitalPotWrite(channel, level);
      
delay(10);
    }
    
// wait a second at the top:
    
delay(100);
    
// change the resistance on this channel from max to min:
    
for (int level = 0; level < 255; level++) {
      digitalPotWrite(channel, 255 - level);
      
delay(10);
    }
  }

}
int digitalPotWrite(int address, int value) {
  
// take the SS pin low to select the chip:
  
digitalWrite(slaveSelectPin,LOW);
  
//  send in the address and value via SPI:
  SPI.
transfer(address);
  SPI.
transfer(value);
  
// take the SS pin high to de-select the chip:
  
digitalWrite(slaveSelectPin,HIGH); 
}

AD5171 Digital Potentiometer


This example shows how to control a Analog Devices AD5171 Digital Potentiometer which communicates via the I2C synchronous serial protocol. Using Arduino's I2C Wire Library, the digital pot will step through 64 levels of resistance, fading an LED.
The I2C protocol involves using two lines to send and receive data: a serial clock pin (SCL) that the Arduino pulses at a regular interval, and a serial data pin (SDA) over which data is sent between the two devices. As the clock pulse changes from low to high (known as the rising edge of the clock), a bit of information containing the address of a specific device and a request for data, is transferred from the Arduino to the I2C device over the SDA line. When the clock pin changes from high to low (the falling edge of the clock), the called upon device transmits it's data back to the Arduino over the same line.
Because the 12C protocol allows for each enabled device to have it's own unique address, and as both master and slave devices to take turns communicating over a single line, it is possible for your Arduino to communicate with many different devices (in turn) while using just two pins of your microcontroller.

Hardware Required

·         Arduino Board
·         AD5171 Digital Pot
·         LED
·         (1) 220 ohm resistor
·         (2) 4.7K ohm resistors
·         breadboard
·         hook-up wire

Circuit

  Connect pins 3, 6, and 7 of the AD5171 to GND, and pins 2 and 8 to +5V.
   Connect pin 4, the digital pot's clock pin (SCL), to analog pin 5 on the Arduino, and pin 5, the data line (SDA), to analog pin 4. On both the SCL and SDA lines, add 4.7K ohm pull up resistors, connecting both lines to +5 V.
   Finally, wire an LED to pin 1, the AD5171's "wiper", with a 220 ohm LED in series.
When the AD5171's pin 6, ADO, is connected to ground, it's address is is 44. To add another digital pot to the same SDA bus, connect the second pot's ADO pin to +5V, changing it's address to 45.
You can only use two of these digital potentiometers simultaneously.

 Schematic


Code

// I2C Digital Potentiometer

#include <Wire.h>

void setup()
{
  Wire.
begin(); // join i2c bus (address optional for master)
}

byte val = 0;

void loop()
{
  Wire.
beginTransmission(44); // transmit to device #44 (0x2c)
                              
// device address is specified in datasheet
  Wire.
send(0x00);            // sends instruction byte  
  Wire.
send(val);             // sends potentiometer value byte  
  Wire.
endTransmission();     // stop transmitting

  val++;        
// increment value
  
if(val == 64) // if reached 64th position (max)
  {
    val = 0;    
// start over from lowest value
  }
  
delay(500);
}

 
Copyright Electronics Projects And Details All Rights Reserved
ProSense theme created by Dosh Dosh and The Wrong Advices.
Blogerized by Alat Recording Studio Rekaman.