ARDUINO-Reading Analog Inputs
A standard Arduino board has six analog inputs (the Mega has 16) and there may not be enough
analog inputs available for your application. Perhaps you want to adjust eight parameters in your application by turning knobs on eight potentiometers.Use a multiplexer chip to select and connect multiple voltage sources to one analog input. By sequentially selecting from multiple sources, you can read each source in turn. This recipe uses the popular 4051 chip connected to Arduino. Your analog inputs get connected to the 4051 pins marked Ch 0 to Ch 7. Make sure the voltage on the channel input pins is never higher than 5 volts:
/*multiplexer sketch read 1 of 8 analog values into single analog input pin with 4051 multiplexer */
// array of pins used to select 1 of 8 inputs on multiplexer
const int select[] = {2,3,4}; // array of the pins connected to the 4051 input
select lines
const int analogPin = 0; // the analog pin connected to the multiplexer
output
// this function returns the analog value for the given channel
int getValue( int channel)
{
// the following sets the selector pins HIGH and LOW to match the binary
value of channel
for(int bit = 0; bit < 3; bit++)
{
int pin = select[bit]; // the pin wired to the multiplexer select bit
int isBitSet = bitRead(channel, bit); // true if given bit set in channel
digitalWrite(pin, isBitSet);
}
return analogRead(analogPin);
}
void setup()
{
for(int bit = 0; bit < 3; bit++)
pinMode(select[bit], OUTPUT); // set the three select pins to output
Serial.begin(9600);
}
void loop () {
// print the values for each channel once per second
for(int channel = 0; channel < 8; channel++)
{
int value = getValue(channel);
Serial.print("Channel ");
Serial.print(channel);
Serial.print(" = ");
Serial.println(value);
}
delay (1000);
}
Analog multiplexers are digitally controlled analog switches. The 4051 selects one of eight inputs through three selector pins (S0, S1, and S2). There are eight different combinations of values for the three selector pins, and the sketch sequentially selects each of the possible bit patterns.
A standard Arduino board has six analog inputs (the Mega has 16) and there may not be enough
analog inputs available for your application. Perhaps you want to adjust eight parameters in your application by turning knobs on eight potentiometers.Use a multiplexer chip to select and connect multiple voltage sources to one analog input. By sequentially selecting from multiple sources, you can read each source in turn. This recipe uses the popular 4051 chip connected to Arduino. Your analog inputs get connected to the 4051 pins marked Ch 0 to Ch 7. Make sure the voltage on the channel input pins is never higher than 5 volts:
/*multiplexer sketch read 1 of 8 analog values into single analog input pin with 4051 multiplexer */
// array of pins used to select 1 of 8 inputs on multiplexer
const int select[] = {2,3,4}; // array of the pins connected to the 4051 input
select lines
const int analogPin = 0; // the analog pin connected to the multiplexer
output
// this function returns the analog value for the given channel
int getValue( int channel)
{
// the following sets the selector pins HIGH and LOW to match the binary
value of channel
for(int bit = 0; bit < 3; bit++)
{
int pin = select[bit]; // the pin wired to the multiplexer select bit
int isBitSet = bitRead(channel, bit); // true if given bit set in channel
digitalWrite(pin, isBitSet);
}
return analogRead(analogPin);
}
void setup()
{
for(int bit = 0; bit < 3; bit++)
pinMode(select[bit], OUTPUT); // set the three select pins to output
Serial.begin(9600);
}
void loop () {
// print the values for each channel once per second
for(int channel = 0; channel < 8; channel++)
{
int value = getValue(channel);
Serial.print("Channel ");
Serial.print(channel);
Serial.print(" = ");
Serial.println(value);
}
delay (1000);
}
Analog multiplexers are digitally controlled analog switches. The 4051 selects one of eight inputs through three selector pins (S0, S1, and S2). There are eight different combinations of values for the three selector pins, and the sketch sequentially selects each of the possible bit patterns.
No comments:
Post a Comment