Go Down

Topic: Looking for a clever hack for using analog 0,1,2 and digital 17 18 19 together (Read 411 times) previous topic - next topic

mearsy25

Hi,

I've got a bit of an issue. I've run out of digital pins (nope, Afraid I can't use a bigger board as I need the Yun stuff). I need to read analog 0 1 and 2 and use analog 3 4 and 5 as digital

I made up a pcb without first testing it on a breadboard. (Silly I know).

My problem is that when I specify

   pinMode(17, OUTPUT);                                                            // Set pin connected to Relay1 to output
     pinMode(18, OUTPUT);                                                            // Set pin connected to Relay2 & 3 to output
       pinMode(19, OUTPUT);                                                            // Set pin connected to Relay4 to output

It affects my analogreads on 0 1 and 2. I just get wacky results.

I realise they are all internally tied to the adc and the arduino cautions switching between analog and digital in close proximity but I misinterpreted that as being switching an analog pin to a digital one during runtime. (Although in a practical circuit I don't see how you could build this).

I am wondering if there is anything that can be amended in something like the wiring.h file to specify the adc should only ever look to analog 0 1 and 2 which would allow me to use digital 17 18 19 as permanent digital pins?

Otherwise it seems a caveat is missing from the documentation which should read. "Don't expect to be able to split your analog pins to be half analog and half digital. Your analog won't work!"

I hope there is a work around. Any experts out there?

Grumpy_Mike

Using the pins in this way will work.
I don't know why you are getting wacky results but what ever it is it is not a function of the arduino itself.
When these pins are switched to digital they are totally disconnected from the analogue input and will not affect it.

So .....

Post your schematic and code for further help.

TomGeorge

Hi,sounds like you have some grounding problems, tracks that are too small or have current running through them associated with your analog inputs and any power current.

Can you please post a  copy of your circuit, in  CAD or picture of hand drawn circuit in jpg, png or pdf format.

Also a picture of your PCB pattern please.

Tom..... :)
Everything runs on smoke, let the smoke out, it stops running......VK3DMK

mearsy25

Thanks all. I can 100% guarantee its not the code. the code has been working flawlessly on an earlier version of this board where i was using digital pins 3 and 4 to drive the relays, however i needed to use those pins for i2c comms so i decided to use a3-a5 as digital outputs  instead.

just changing the board to the one pictured below and changing the code to set those pins to output and use them as digital write caused this issue

I first noticed it on analog 0. I use a lcd keypad shield where 5 buttons are run through various resistors to analog 1. when a button is pressed it gives a different value to the adc. this is how we work out which of the buttons is being pressed. I found with the new board the reading were in a very odd range. Buttons near the top of the arc range still worked as did the ones at the bottom. but in the middle of the range it didn't detect the presses. similarly my readings on a1 and a2 of my lm335 temp sensors jumped around a lot.

its almost like the issue you see of switching analog reads quickly where you need to allow the arc time to settle before the next read. but I had thought that this delay needn't apply when specifying the pins as digital


mearsy25

P.S If I comment out the pinMode(17/18/19, OUTPUT); and also comment out the digital writes to those pins. analog 0 1 and 2 work perfectly as expected so its definitely not the code itself.

Grumpy_Mike

Quote
Thanks all. I can 100% guarantee its not the code. the code has been working flawlessly on an earlier version of this board where i was using digital pins 3 and 4 to drive the relays, however i needed to use those pins for i2c comms so i decided to use a3-a5 as digital outputs  instead.

Now this is odd because the I2C interface does not use digital pins 4 & 5 but analogue pins 4 & 5.

So no schematic or code then, I thought you wanted help?

mearsy25

Ah but mike. Not on the Leonardo or Yun. On those its pins 2 and 3 which are used for i2c.

I've not drawn up a schematic. I just posted the board layout with the arduino on top to show the pinouts.

As I say, its not the code. the code works fine on other pins and it works fine when i disable specifying pin mode output and using the other 3 digital pins.


I can't post the code as it contains proprietary algorithms.

Grumpy_Mike

Quote
Ah but mike. Not on the Leonardo or Yun. On those its pins 2 and 3 which are used for i2c.

You kept that quite, true you did hide away a clue by saying:-
Quote
Afraid I can't use a bigger board as I need the Yun stuff

But that did not flag it up to me.
This is the wrong section is it not? Should it not be in the Yun section?

Quote
I can't post the code as it contains proprietary algorithms.

Fine, so post a simple cut down code that illustrates the problem.

mearsy25

Possibly but its not Yun specific. I figure the same would happen on the leonardo so its not really a gun section specific query. its more to do with analog read on a 32u4.

I will try and  get some time to cut it down to a working code example which shows the issue.

Go Up