I don't think the code is my problem, my weakness is the circuitry. Basically what I want to do is read from 12 analog sources and turn on or off a corresponding digital output. So I am using a 4051 analog multiplexer to read 8 sources. That takes one analog input pin on my Arduino Diecimila, the remaining 4 sources are read directly by the remaining analog input pins. The 4051 chip also requires 3 digital output pins to function, that leaves me with 11 digital output pins but I need 12.
Can anyone think of a better way to tackle this problem? Any advice would be greatly appreciated!
(Did I mention this needs to be doubled with 1 Arduino controlling it all if possible?)
Good ideas! My problem with the 4051's is that I get the same reading no matter which pin I select. If someone could post a schematic of how it should be wired that would help immensely. In the mean time I'll try to find the guide I followed for using photocells...
A little more detail, I'm using the light sensors to detect an object through 1/8" clear acrylic. thought about using ultrasonic sensors to do this but they are far more expensive
Just checking in on my lunch break. Yes the photocells/photoresistors are light driven resistors. I will work on getting a schematic for the entire lay out I'm trying to use, does anyone know of a program that makes drawing circuits a little easier? I don't want to have to free hand or use Paint to do this.
This is how to connect the LDR's as voltage dividers :
just substitute his use of Analog pin 0 with the inputs on the 4051.
You will need an extra resistor for each LDR to make the voltage divider. In the example above he is using 100k resistors you might have to experiment with that value depending on the resistance of your LDR's.
Do you get OK readings from the LDR's connected to the 4051 ?
Extending that circuit with a second 4051 would only require the use of one extra analog in on the Arduino, because you can use the same 3 digital outs to address both 4051's
There actually is a connection from the 5v source just above that pin, for some reason the line was removed and I didn't catch it before I posted, sorry for the confusion.
I've got a bunch of stuff hanging off the other digital out pins and 5v source, I'm going to isolate the circuit to just be what I posted pics of and let you guys know the results. Thanks for taking the time to look.
Ok, When I run it each light sensor reads 290 - 292, pretty much the same value. I put lights over some of the sensors and check readings again and they're the same, between 290 and 292 on every sensor. To sum up the code below, we're going in binary order checking all 8 of the photoresistors 3 times each and printing that out. I modified some code I found on the playground:
The circuit is as shown before but with a few devices connected to a zener diode on the 5v source.
/*
codeexample for useing a 4051 * analog multiplexer / demultiplexer
by david c. and tomek n.* for k3 / malmö högskola
*/
int led = 13; //just a led
int r0 = 0; //value select pin at the 4051 (s0)
int r1 = 0; //value select pin at the 4051 (s1)
int r2 = 0; //value select pin at the 4051 (s2)
int row = 0; // storeing the bin code
int count = 0; // just a count
int bin [] = {
000, 1, 10, 11, 100, 101, 110, 111};//bin = binär, some times it is so easy
int val = 0;
void setup(){
I thought his code had flaws when I ran it, but since it was on the playground I assumed it was correct... it's repeating the read from channel 100 3 times isn't it? (1--, 10-, and 100 are all the same right?)
The repeating 3 reads that I want to have are from this loop:
while (xxx< 3){
val = analogRead(0);
Serial.print("Pin: " );
Serial.print(count );
Serial.print(" Value: ");
Serial.println(val );
xxx++;
delay(100);
}
That works just fine, I want to gather the data 3 times just to be sure I've got the right one. This part will be removed after the troubleshooting phase.
Can anyone explain whats going on in this code segment right here?
r0 = row & 0x01;
r1 = (row>>1) & 0x01;
r2 = (row>>2) & 0x01;
I know it's setting the digital pin values, but I don't understand the logic.
I changed these parts to use pins 2-4 for channel selection since 0 and 1 will not work. Thanks for pointing that out, I had no idea. Can I use those pins as long as I do NOT use the serial port? Speaking of interference, would using UTP wires (found in CAT5) cause problems? Should it be shielded wire instead?
Anyway, after those changes to the code and reworking the wire to the correct pins there was no change in the readings. All sensors still read in the 290s no matter what kind of lighting I use.