Analog pin descrepancy

I'm running a Solenoid circuit through a transistor with an outside power source with my Osepp Uno R3. I have digital pin 9 connected through a 1k resistor to the base, and this pin is pwm on the R3. Utilizing analogRead(), I can tell that the value on the pin is about 400 when I first load the sketch and evens off to around 280 whether I write a value of 0 or 1000 to the pin. As long as the pin is connected to the base of the transistor, the circuit runs, but writing a value of 0 will not turn it off. Disconnecting the power source OR the digital pin will turn off the solenoid circuit. This tells me my circuit is correct, and that my pin could be outputting power I didn't intend.

What could be causing this? Is it possible I've damaged the board in some way? I'm a total noob so I immensely appreciate any advice or insight.

Here is the code:

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
if (Serial.available() > 0)
{
  int inByte = Serial.read();

  switch (inByte)
  {
    case 'w':
      analogWrite(9, 0);
      Serial.println("Turned off");
      break;
    case 'q':
      analogWrite(9, 1000);
      Serial.println("Turned on");
      break;
    default:
      Serial.println("No command recognized.");
      int val = analogRead(9);
      Serial.println(val);
      break;
  }
}

}

Can you post schematic of how everything is wired?

I was unaware that you could analogRead() pin 9.

Hi,

whether I write a value of 0 or 1000

the values of 0 to 255 are valid PWM outputs.

Tom..... :slight_smile:

Ok, I've tested this, and of course not - you cannot analogRead pin 9. the output is similar to a floating pin.

Eddie, what are you trying to do with this? Try putting the long lead of a LED into pin 9, twisting the short lead to a 330 ohm resistor, and putting the other end of the resistor into the GND pin.

When you type 'q' the led turns on, and when you type 'w' it goes off. As expected - and your messages get printed in the serial monitor. When you type any other character, you get a random number centered around 500-ish printed on the serial monitor.

Pins 2 through 13 can be used for digital input only, so analogRead will not have any meaning. Pins A0 through A5 can be used either with digitalRead or analogRead.

ChrisTenone:
I was unaware that you could analogRead() pin 9.

Of course you can. It's analog channel 9 and not pin 9. Now there is indeed a problem as there is no analog channel 9 on the Uno 8)

sterretje:
Of course you can. It's analog channel 9 and not pin 9. Now there is indeed a problem as there is no analog channel 9 on the Uno 8)

Right. But in the OP's code, he's trying to read pin 9 (well, to be honest, he just says analogRead(9) but in context ("I'm a total noob" along with "analogWrite(9, 1000)) it appears that Eddie is attempting to analogRead pin 9, which of course is meaningless.

Hopefully theEddie will return to this thread.

Hi,
I can't see any
pinMode statements....

Tom.... :slight_smile:

pinMode isn't needed for analogRead, the IDE does that for you.
Would be more helpful if the IDE warned that D9 did not support analogRead on an Uno, just as a warning about = vs == in an if ( ) would be helpful all around, but you know how it goes ...

Hi,

What about pinMode(9,OUTPUT).

It would be nice to promote code writing makes sense, not assumes it.

I would be declaring my input and output pins in setup, just to be tidy and to indicate that we all know the I/O pins being used.

I never ASSUME I/O, please use pinMode, a programmer went to a lot of trouble to provide that statement.

If you look at the code the OP provided, what are the I/O pins. Delving into code to find the I/O allocations is wasteful and confusing.

Tom.... :slight_smile:
PS. Do me a favor and declare the I/O in your setup please......

TomGeorge:
What about pinMode(9,OUTPUT).

Also not needed for analogWrite.

Hi,
Eddie.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Also do you get a value back when you send a 100, don't forget your valid PWM is 0 to 255, not 0 to 1000.

Tom... :slight_smile:

The IDE does (sort of) warn about = vs ==

sketch_dec12a:5: warning: suggest parentheses around assignment used as truth value 
   if(a = 1)Serial.println("Ooops");

           ^

It doesn't explicitly say "You should have used ==" because there's no way for it to know that == was intended. But it does point out that there's a potential problem in the statement.

However, there are 3 further problems:

  • Many people don't have verbose option turned on in Preferences
  • The warning in your code is followed by a lot of output from compilation of the libraries which effectively buries it unless you specifically scroll back through the output.
  • And many people ignore the warnings anyway.

It might help if there was a summary line at the end which listed how many errors and how many warning messages were issued.

Pete

I'd like to see a hardware solution inside the avr chips: Every i/o pin could be used as digital input and output with digitalRead() and digitalWrite(), pwm output with analogWrite(), or as analog input with analogRead(). And pinMode would be unnecessary because using digital/analogRead or digital/analogWrite statements would set the mode internally.

Pie in the sky? Maybe not now that Microchip is making them.

ChrisTenone:
I'd like to see a hardware solution inside the avr chips: Every i/o pin could be used as digital input and output with digitalRead() and digitalWrite(), pwm output with analogWrite(), or as analog input with analogRead(). And pinMode would be unnecessary because using digital/analogRead or digital/analogWrite statements would set the mode internally.

Pie in the sky? Maybe not now that Microchip is making them.

ah, you want an AVR with built-in JIT C-compiler :smiley:

Sorry for the late reply, apparently I'm using the wrong pin. The reason I tried to switch to analog was because when the digital output was set to 'LOW' it was still activating the transistor. I thought analogread() on the pin was viable as it is a pwm pin but, again, I am mistaken. I will re-examine what I'm doing and be back soon.

I just want to say thanks to everyone for taking Time to look at my problem. So I switched the analog statements to digital statements, and the digitalread() function returns a value of 1 when on an 0 when off. So according to that, the pin is functioning as it should, yet it is still apparently running current when off as the transistor is engaging the solenoid.

Several have asked for schematics, is there newb friendly programs for that?

Also someone's asked what I'm intending to do. I am literally only trying to run a solenoid off an outside power source with a microcontroller. I have two 9v batteries wired in series as a power source. It's a simple project.

You can hand draw a schematic on a piece of paper with a pen and make a scan or photo of it that you post here.

Let me know if this doesn’t make sense

You're at least missing a base resistor. Or is that an omission in your schematic?