Author Topic: Help with analog input, multiple buttons dilemma  (Read 23735 times)

0 Members and 1 Guest are viewing this topic.

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Help with analog input, multiple buttons dilemma
« on: May 25, 2012, 10:36:32 pm »
I tried several days to get a solution to my dilemma, maybe some help will be useful. Long history short: I have a electric thing to put towels (http://goo.gl/kCeCZ) uses little energy but montly it is more than 1 towel, so I want to add a timer like an oven to this device to push a button and program let say, 5 hours.

OK. My current idea (I know I can use a mechanical timer or something like that but I wish to make a little device with a small attiny to learn):


The sensor is an inductance sensor to detect when the momentary button is pressed (after relay is connected by the attiny so in this way the relay works like a 'life-supporting' device and the attiny can kill itself).

So the first attached photo shows the current circuit, my small relay board, inductance sensor so I need to finish the software now. Here is the problem. This attiny has 4 analogs, 2 pwm. As I don't have an HV programmer I have 3 analogs, but I am using 1 as an output (2 leds and relay), one analog input pin for the current sensor and I want to connect a 8-dip-switch in the last remaining pin.

Base delay is 10s (instant off), and every switch will add some time to have a total of 16 hours max (8 bits):


Second attached photo shows the dip switch and my test environment with an arduino, I am guessing this is possible with some combination of current dividers and maybe another idea? (attiny IO is 10 bits so I think I can reach 8 bit resolution including some noise of the reads)

Last attached photo shows the attiny in the usb programmer, it is working well but I am debugging first the logic in an arduino because it is a lot easier with the usb serial.

So, any recommendation/idea?

Thanks!
My website: http://ried.cl
 

Offline caroper

  • Regular Contributor
  • *
  • Posts: 193
  • Country: za
    • Take your PIC
Re: Help with analog input, multiple buttons dilemma
« Reply #1 on: May 25, 2012, 11:57:45 pm »
If you only have one pin left, and as time is an analogue quantity, why not use a POT, rather than switches and have the ADC set the delay according to the POT setting?


Cheers
Chris


Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #2 on: May 26, 2012, 12:12:32 am »
If you only have one pin left, and as time is an analogue quantity, why not use a POT, rather than switches and have the ADC set the delay according to the POT setting?


Cheers
Chris

Well because I don't think the pot will be as precise as 8 different switches for the time. If this approach is possible I easily can have at max 256 combinations (1 sec to 16 hours with 5 mins intervals), maybe a pot will work with a wide knob and a little more than 1 degree per 5 mins but I think this can be done with the 10 bits precision of the analog leg
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #3 on: May 26, 2012, 05:39:37 am »
One way to convert the 8-position DIP switch's setting to analog is to use an R2R ladder network.  To keep your circuit as simple as possible, each position of the DIP switch would need to be single-pole, double-throw.  See the first figure on this webpage for the basic idea.  (The op amp's not necessary.)

If your DIP switch is SPST then it won't be as simple since you'll need to introduce extra circuitry.  A MCU with more I/O pins may be your best bet.  Or buy a DIP switch with SPDT switches.  They're not very common, however.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #4 on: May 26, 2012, 07:13:57 am »
One way to convert the 8-position DIP switch's setting to analog is to use an R2R ladder network.  To keep your circuit as simple as possible, each position of the DIP switch would need to be single-pole, double-throw.  See the first figure on this webpage for the basic idea.  (The op amp's not necessary.)

If your DIP switch is SPST then it won't be as simple since you'll need to introduce extra circuitry.  A MCU with more I/O pins may be your best bet.  Or buy a DIP switch with SPDT switches.  They're not very common, however.

Wow! thanks a lot, I was looking for the "theory" related with this!
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #5 on: May 26, 2012, 08:59:39 pm »
As you can see from reading the Wiki article, doing this with 8-bits is going to require fairly precise resistors.  If this is a one-off project then you can handpick the resistors.

And, as I mentioned, if you are use a DIP switch with SPST switches you'll need extra circuitry. 

Because of these factors you may be better off just using a MCU with more I/O.

Assuming you go this route then the best way that I can think of to drive the ladder using SPST switches is to use an octal buffer/driver such as the 74HC241.  (This part has a tri-state feature that you wouldn't need; you always want the output to be 0V or Vcc.)  See the attached schematic for the basic idea.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #6 on: May 27, 2012, 06:31:34 am »
As you can see from reading the Wiki article, doing this with 8-bits is going to require fairly precise resistors.  If this is a one-off project then you can handpick the resistors.

And, as I mentioned, if you are use a DIP switch with SPST switches you'll need extra circuitry. 

Because of these factors you may be better off just using a MCU with more I/O.

Assuming you go this route then the best way that I can think of to drive the ladder using SPST switches is to use an octal buffer/driver such as the 74HC241.  (This part has a tri-state feature that you wouldn't need; you always want the output to be 0V or Vcc.)  See the attached schematic for the basic idea.

So the main issue is to not left pins floating with this dip switch right? I saw a robot kit http://www.arexx.com/downloads/asuro/asuro_manual_en.pdf that detects 6 switches (so maybe 8 it is possible, I don't need high speed response in fact I can take a lot of iterations to get the data, let say 1 minute (the coil to measure the current also requires lot of iterations, it is not very precise, in my tests like 100 ms of iterations gets a precise response about if the switch is down or not). I will try sóon.
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #7 on: May 27, 2012, 06:57:57 am »
Yes, if you use the R2R ladder then each node of the ladder needs to be driven by either 0V or Vcc (5V in your case.)  There might be a better way to do it than the R2R ladder.  The schematic in your link is doing something different.  I don't know if they can discern between multiple switch combinations (which is what you need) or just a single switch.
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #8 on: May 27, 2012, 07:41:37 am »
I just looked a little closer at that PDF you linked to.  They do indeed discern between multiple switch combinations.  That's interesting.  It must not be terribly accurate judging from what they say about needing to call the function several times to get a correct answer.

They must be determining the switch positions based on how the capacitor charges up.
 

Offline richcj10

  • Supporter
  • ****
  • Posts: 201
  • Country: us
Re: Help with analog input, multiple buttons dilemma
« Reply #9 on: May 31, 2012, 03:11:34 pm »
Why not a voltage divider circuit and enabling less or more resistors adjusts voltages...
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #10 on: May 31, 2012, 07:38:57 pm »
Why not a voltage divider circuit and enabling less or more resistors adjusts voltages...

I went through that particular train of thought before.  I couldn't come up with a satisfactory solution.

I'd like to see a schematic for your idea.  Remember, he has an 8-position SPST DIP switch and a 10-bit ADC.  That's 256 different switch combinations and an ADC value that ranges from 0 to 1023.  Although, his application can use oversampling, so he can probably get a 12-bit ADC value at least.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #11 on: May 31, 2012, 09:45:04 pm »
I will soon test the thing in the asuro pdf, seems very smart and I think I can get the 8 bit with the SPST DIP switch, I am just sorting out some resistors to start when I have a free time window :) I will post any result, it will be very interesting to get 8 bit or 10 from switches, I can imagine this little attiny with 40 switches! HAHA

Why not a voltage divider circuit and enabling less or more resistors adjusts voltages...

I went through that particular train of thought before.  I couldn't come up with a satisfactory solution.

I'd like to see a schematic for your idea.  Remember, he has an 8-position SPST DIP switch and a 10-bit ADC.  That's 256 different switch combinations and an ADC value that ranges from 0 to 1023.  Although, his application can use oversampling, so he can probably get a 12-bit ADC value at least.
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #12 on: June 01, 2012, 01:28:31 am »
I thought you only had one pin left.  The Asuro schematic is using two pins for the 6 switches.  I haven't looked at the code but I imagine one is for ADC and the other is for discharging the cap.

What are each of the pins on your ATtiny assigned to?  Are the ISP pins dedicated to programming?  Because if they are then there are ways to use them for other purposes.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #13 on: June 01, 2012, 07:25:10 am »
I thought you only had one pin left.  The Asuro schematic is using two pins for the 6 switches.  I haven't looked at the code but I imagine one is for ADC and the other is for discharging the cap.

What are each of the pins on your ATtiny assigned to?  Are the ISP pins dedicated to programming?  Because if they are then there are ways to use them for other purposes.

No discharging pin, one pin is for interruption mode, and one is for polling via code, both can work independently. I think this design is wondeful, there is a small sleep time in the code to allow the cap discharge so probably all is very open to tweaking.

Right now:
1 pin is for the relay
2 pins are for a dual color led (red-green), sounds overkill wasting a pin in this but I want to use this device and know what is doing without a screen
1 analog input is for the induction sensor coil
gnd, vcc and reset other 3 pins (reset can be an extra IO but it is not very convenient)
1 pin left is for the 8 switches
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #14 on: June 01, 2012, 07:31:29 am »
Ah!  I see.  I'll have to look at the code.  It sounds interesting.
 

Offline SeanB

  • Super Contributor
  • ***
  • Posts: 16366
  • Country: za
Re: Help with analog input, multiple buttons dilemma
« Reply #15 on: June 01, 2012, 03:11:43 pm »
You can drive the 2 leds with one pin, just put a pair of resistors across the supply to maker a half volt rail ( use 1k or so and high efficiency leds) and connect the leds in anti parallel in series with the micro output and the resistors. You can have off, red, green or orange by either tristate, high, low or oscillating the pin at 100Hz or so.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #16 on: June 02, 2012, 12:26:43 am »
You can drive the 2 leds with one pin, just put a pair of resistors across the supply to maker a half volt rail ( use 1k or so and high efficiency leds) and connect the leds in anti parallel in series with the micro output and the resistors. You can have off, red, green or orange by either tristate, high, low or oscillating the pin at 100Hz or so.

I tried first with a led with 2 colors and 2 leads but it was not working properly for some reason (I am very noob in electronics), I think the "fake ground" that the tiny makes is very weak or something like that I can't explain. The first simple circuit here: http://www.neufeld.newton.ks.us/electronics/?p=151 is the one you say?
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #17 on: June 02, 2012, 02:52:54 am »
I've been analyzing the Asuro circuit.  I wrote hacked-out a Perl script to determine the node voltages for each of the switch positions and calculated what the ADC value would be and used this value with the PollSwitch() function to see what it returns.  (I had to fix in bug in their calculation.)

I does indeed work.  If everything is nearly perfect, that is.  Nice thing about your case is that you're not building thousands of boards with the circuit so you can tweak the component values and code to suit your needs.

You will need at least 12 bits of resolution to use it with an 8-position switch.  That's assuming everything else is perfect--the MCU's ADC, the resistor values, etc.  Because of the circuit's design, only the top half of the ADC values are used.  That's basically throwing away half of the possible values.  Yes, that's just one bit but it is the most significant bit!  If you're going to throw a bit away throw away the least significant.

If you can free that pin up by driving the LEDs with just one pin then it will help you a lot since you can then divide the 8 switches into 2 groups of four which will be much more reliable.

I've attached the Perl script I used if you want to double-check my analysis or do your own.  Tweaking the values of the various parameters can be very informative.  I've also included the script's output for a couple of test cases: 6 switches, 10-bit ADC; 8 switches 10-bit ADC; 8 switches 12-bit ADC; 4 switches, 10-bit ADC.
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #18 on: June 02, 2012, 05:09:52 pm »
I've been analyzing the Asuro circuit.  I wrote hacked-out a Perl script to determine the node voltages for each of the switch positions and calculated what the ADC value would be and used this value with the PollSwitch() function to see what it returns.  (I had to fix in bug in their calculation.)

I does indeed work.  If everything is nearly perfect, that is.  Nice thing about your case is that you're not building thousands of boards with the circuit so you can tweak the component values and code to suit your needs.

You will need at least 12 bits of resolution to use it with an 8-position switch.  That's assuming everything else is perfect--the MCU's ADC, the resistor values, etc.  Because of the circuit's design, only the top half of the ADC values are used.  That's basically throwing away half of the possible values.  Yes, that's just one bit but it is the most significant bit!  If you're going to throw a bit away throw away the least significant.

If you can free that pin up by driving the LEDs with just one pin then it will help you a lot since you can then divide the 8 switches into 2 groups of four which will be much more reliable.

I've attached the Perl script I used if you want to double-check my analysis or do your own.  Tweaking the values of the various parameters can be very informative.  I've also included the script's output for a couple of test cases: 6 switches, 10-bit ADC; 8 switches 10-bit ADC; 8 switches 12-bit ADC; 4 switches, 10-bit ADC.

Very nice, so 10 bit will not be enough for 8 switches :o
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #19 on: June 02, 2012, 07:52:00 pm »
Very nice, so 10 bit will not be enough for 8 switches :o

Nope.  Afraid not.  You might be able to get the resolution you need using oversampling.  On an ATtiny that may be a trick.

Can you can describe how the LED is going to be used and how it is wired?
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #20 on: June 02, 2012, 10:49:49 pm »
Hey thanks a lot you are always very helpful, I think Dave should send you some gifts haha! Well about the device, maybe it is a good idea to drop 1 led or retry to drive both with 1 leg, I have some light sequences in mind, but not a final diagram of all the states, 3 color seems just fine to me to achieve: 1) Clearly know when the timer starts, 2) warnings about finishing (10 mins-5 mins, 10 sec?) 3) Some "mode" like if you hold the same button that started the timer (sensed thanks to the induction sensor) led blinks and you can reset or change the time? something like that.

Nevertheless I think I didn't get what the asuro code is doing:
http://pastebin.com/3QF8C19S

Can you explain the sequence in easy words? With the 4.7 uF cap it seems like a duracell :P the values drop very slowly (I had only 3 switches in the protoboard right now), I tried lowering the capacitor but 0.47 uF also is too high and with very small caps (tested a 33 pF) the value it is very oscillating, I am using wiring library for the arduino

Code: [Select]
  pinMode(A0, OUTPUT);
  digitalWrite(A0, HIGH);
  delay(10);
  digitalWrite(A0, LOW);
  pinMode(A0, INPUT);
  return analogRead(A0);
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #21 on: June 03, 2012, 05:14:04 am »
You can get rid of all that charging/discharging stuff.  I don't know why they did that.  You will have something like the attached for your circuit.  If you are reading 3 switches then use 2K, 4K and 8K for the resistor values.  The capacitor doesn't need to be nearly as big as what you used.  10nF to 100nF should be fine.

All you'll have to do then is configure the pin as analog and read the value.  To figure out which switches are closed you'd just do like they did in their code.  (Sorta.  They have a bug.)

For three switches you'd say:

Code: [Select]
uint8_t SwitchSettings = ((10240000 / AdcValue - 10000) * 8 + 5000) / 10000;
AdcValue is the value returned from analogRead();

The template for this that will work with any number of ADC bits and any number of switches (up to eight) is:

Code: [Select]
uint8_t SwitchSettings = ((<10000 * 2^NumAdcBits> / AdcValue - 10000) * <2^NumSwitches> + 5000) / 10000;
Just fill in the information in brackets.  The bug they have is that they are multiplying by 63 when they should be using 64.  This is the number of switches or 2^6.

I'm going to play around with the circuit some more tomorrow.  I want to create some graphs to see how the nominal node voltages are distributed and see if the above equation is appropriate.  It may need some tweaking.  I'll let you know what I find out.

Edit: Fixed template code.
« Last Edit: June 03, 2012, 08:01:12 am by TerminalJack505 »
 

Offline Erwin RiedTopic starter

  • Regular Contributor
  • *
  • Posts: 206
  • Country: no
Re: Help with analog input, multiple buttons dilemma
« Reply #22 on: June 03, 2012, 06:38:52 am »
WOW! thanks a lot again! I will assemble that tomorrow! great help! I hope the device works as I expect and my towels stop smelling 8) this was the final step besides programming the attiny
My website: http://ried.cl
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Help with analog input, multiple buttons dilemma
« Reply #23 on: June 03, 2012, 07:56:53 am »
Oops.  Just notices I had an error.  I should have said...

Code: [Select]
uint8_t SwitchSettings = ((<10000 * 2^NumAdcBits> / AdcValue - 10000) * <2^NumSwitches> + 5000) / 10000;
...for the generic equation.

Right now my code is still in Perl so I haven't tried that in C yet.  Here's what I have in Perl:

Code: [Select]
return int ((2**$ADCBits / $ADCValue - 1.0) * 2**$NumSwitches + 0.5);
And when you convert that to do everything in integer math like they did in the Asuro code it should like the C code above.
 

Online PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5214
  • Country: nl
Re: Help with analog input, multiple buttons dilemma
« Reply #24 on: June 03, 2012, 09:27:12 am »
1) Clearly know when the timer starts
2) warnings about finishing (10 mins-5 mins, 10 sec?)
3) Some "mode" like if you hold the same button that started the timer (sensed thanks to the induction sensor) led blinks and you can reset or change the time? something like that.

1) Turn the led on
2) Blink the led
3) You will not be able to detect the button when the relay is powered

So, just one led -> one I/O port saved  :)
Keyboard error: Press F1 to continue.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf