I have some questions about SN74LS151N digital multiplexer.
I want to conect to the mux 8 phototransistors, then I want to power up those from external power source of 5V. After that, I want to connect S0 S1 S2(select inputs) to digital pins from arduino mega 2560 to generate the code from 000 to 111. After this stage I want to connect mux OUTPUT to another digital pin from arduino to read the OUTPUT data, and show me in serial monitoring or on a LED diode.
I don't have programming skills in C but I a know basics
If someone can help me with an exemple or source code I would be pleased!
Doesn't mega have large number of analog pins? Why not use 1 pin/phototransistor?
If they're being used as digital, then connect all to a shift-in register, clock in the state of all at 1 time, and shift into the Mega.
It have a 14 or 16 analog pins but i think to connect the phototransistor as you said 1pin/phototransistor to digital because i think its much easier to control it.
If your phototransistor setup has a very definite on/off state (for example, depending on whether a beam of light is blocked or not), then a digital input is adequate. You just need to find a suitable value of pullup or pulldown resistor so that the voltage on the pin is reliably 1.5V or less in one state and 3V or more in the other state.
Here is an example of the code you might use to read and display 8 inputs (warning: untested code!):
const int S0pin = 2, S1pin = 3, S2pin = 4;
const int photoPin = 5;
void setup()
{
pinMode(S0pin, OUTPUT);
pinMode(S1pin, OUTPUT);
pinMode(S2pin, OUTPUT);
Serial.begin(9600);
}
bool readPin(unsigned int pin)
{
digitalWrite(S0pin, (pin & 1) ? HIGH : LOW);
digitalWrite(S1pin, (pin & 2) ? HIGH : LOW);
digitalWrite(S2pin, (pin & 4) ? HIGH : LOW);
int val = digitalRead(photoPin);
return (val == HIGH);
}
void loop()
{
for (unsigned int i = 0; i < 8; ++i)
{
bool pinVal = readPin(i);
Serial.print(pinVal ? 1 : 0);
Serial.write(' ');
}
Serial.println();
delay(1000);
}