I know similar questions have been posted in the past, but I haven't seen the specific answer I'm looking for.
All of my digital pins are assigned to other functions, so I need to use the A5 analog pin as a digital input pin for reading a push button switch. I have the switch connected to ground and to A5. I'm using the following code:
pinMode(19, INPUT);
digitalWrite(19, HIGH);
I have an LED on pin 14 (A0). When I use the code:
digitalWrite(14, ! digitalRead(19))
nothing happens when I press the switch. At one point, this code did work, but suddenly it stopped. I tried ohming out the circuit and the wiring is all fine. When I was checking the voltage on the pins with a volt meter, the meter itself caused A5 to read a low and make the LED change, implying that I had a broken ground, or that A5 was a floating input.
I tried reading the pin with analogRead, and then comparing to see if the digital value was less than 100 with:
switchPin = analogRead(5) < 100;
This works, but I don't like not knowing what's going on. I'm using an Uno clone called the Japanino, from the Japanese kit maker Gakken.
So, my questions are: Was I doing something wrong with my original digitalRead code? Does the Arduino have internal pull-ups on the A0-A5 pins? Is it possible to burn out the pull-up just by connecting it to ground through a switch? Should I be using an external pull-up resistor just in case? Thanks.
I have an LED on pin 14 (A0). When I use the code:
digitalWrite(14, ! digitalRead(19))
First of all you should post your complete sketch. By just going with what you show we have no idea if you performed a
pinMode(14, Output); statement or not. It won't work without that.
Also just because of my self appointed role on this forum ( ;)), can I ask if you have a resistor wired in series with your LED? A wiring drawing could be a help us to determine if you have everything wired correctly.
As far as your question about using analog input pins as digital pins, and if they have internal pull-up resistors avalible, the answer is yes. The bible for this kind of questions is the Atmel avr mega328 datasheet. Here is a small extract on the subject:
All AVR ports have true Read-Modify-Write functionality when used as general digital I/O ports.
This means that the direction of one port pin can be changed without unintentionally changing
the direction of any other pin with the SBI and CBI instructions. The same applies when changing
drive value (if configured as output) or enabling/disabling of pull-up resistors (if configured as
input). Each output buffer has symmetrical drive characteristics with both high sink and source
capability. The pin driver is strong enough to drive LED displays directly. All port pins have individually
selectable pull-up resistors with a supply-voltage invariant resistance. All I/O pins have
protection diodes to both VCC and Ground as indicated in Figure 13-1. Refer to ”Electrical Characteristics”
on page 314 for a complete list of parameters.
For the wiring diagram, I'm not good at drawing those in text. Basically, it's just the normally open pushbutton switch - one side to ground, the other to A5; and the LED - cathode to ground, anode directly to A0. No resistor for the LED, I'm using the internal buffering of the Japanino to limit LED current.
and the LED - cathode to ground, anode directly to A0.
Then I will have no further correspondence with you until you wire a series resistor to your led, 200-1,000 ohms will work fine. When you light up the led your Arduino output pin is crying out in pain, yes it lights the light, but it is in great peril of being destroyed if not now then the very next time you use it. It's ok as when the pin does die you can purchase a replacement processor chip with preinstalled bootloader code for around $6.
PS: when you get the resistor you might change the pin used for the led, just to make sure it's not with us any longer. I think your code should be fine.
[edit]Edit: OK I see your part about this being a 'Japanino' board. I have no knowlege of that board or how it tries and limit current flow. Perhaps a link to it's schematic? I have searched a little and can find no schematic, just a picture. Looking at it seems to be a pretty standard arduino, with no special current limiting or protection for the output pins. You need a resistor unless you can prove otherwise! Vol.27 テクノ工作セット(8ビットマイコン+光残像キット) | 大人の科学マガジン | 大人の科学.net [/edit]
Ok, I will add the resistor for the next time. Right now, the LED was just used for confirming whether the switch was being seen as opening and closing to the A5 pin. Which brings me back to whether I need to drop in an external pull-up resistor. Using analogRead() and comparing the result to 100 does work, but I don't like it. If the internal pull-up is missing or was burned out, then I'll just use the external 20K ohm resistor and go back to using digitalRead().
I haven't seen a schematic for the Japanino yet. If I do find it, I'll make the link available to the forum. In the book that comes with the kit, the publisher's editors visited Italy to watch the Ardunio being made, and took pictures of themselves with the Arduino staff. So, the hardware is probably identical to the Uno, with the exception of 4 additional pins - two more analog pins, A6 and A7; and another 5V pin and N/C pin on the D8-D13 bank. I think all Gakken did was ask for new silk screening to add the Japanino logo to the Uno.
The Japanino has a soldered on atmega chip. Smoke that and you'll pay an ubergeek to solder one with a boot loader on unless you're capable of doing that yourself. Also problematic is programming the chip yourself since it's a quad flatpack. You can solder in the missing ICSP pins on the Japanino and buy another Japanino or Arduino to program the chip with.
This is why the Arduino is socketed, you can fry the chip during development and just replace the socketed part. The ICSP pins are present so if you have another Arduino you can just program it. http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1165363464
Then how can you use it? An Arduino board isn't a toaster or a table-lamp that you just plug in and turn the switch.
Arduino compatible. Design and program for the Arduino and the shields and sketches will work on the Japanino. As mentioned before, the only real difference is that the Japanino has two extra analog pins and another 5V pin. What I don't know is the whether the Japanino has that internal pull-up resistor on the analog pins, or if it got fried on the A5 pin. Doesn't matter much, I just got some 20k ohm resistors from a shop in Akihabara and I'll try experimenting with them tonight or tomorrow.
For why you ALWAYS need a resistor (or constant current driver) with an LED see:-
Glad to see you back posting GM after not seeing any from you for a couple of days. Probably on Vacation to the Canary Islands or something?
Anyway glad you are back so I don't have to be the sole enforcer of 'leds need resistor' meme. It's been somewhat of a burden with all the new arduino Christmas presents being used. Twice they have posted pictures from one of the Arduino tutorials showing a arduino NG board and a led plugged directly into the pin 13 and ground pins. Ugh.....
Quote:
I haven't seen a schematic for the Japanino yet.
Then how can you use it? An Arduino board isn't a toaster or a table-lamp that you just plug in and turn the switch.
That's true, but some of us software bozos have built some pretty complex stuff without ever getting near a schematic. I can set pins high and they stay high and I can drive them low by connecting to ground with a switch; the other way around won't work without extra circuitry. If I just play by those rules I don't have to understand why; things will work. I need to consider an LED as 2 parts that are both required; why is not that important to me. As long as I always add the resistor when I use an LED, it works and doesn't kill my Arduino. If I want to interface to something else, I come here and search and find what is recommended. Don't get me wrong - I am fine with having to breadboard or solder parts and a have a nice assortment of resistors, capacitors, diodes, switching transistors and voltage regulators. Why does a voltage regulator need a capacitor? I have no clue. But it does; that's the rule. Follow it and you get power.
I am a cook, not a chef, when it comes to circuits.
Why does a voltage regulator need a capacitor? I have no clue. But it does; that's the rule. Follow it and you get power.
Well there is a way to gain knowledge of such things rather then just follow rules that may or may not be applicable or accurate for your use of the device. Keep in mind that not all information posted on the web is accurate or at least easily misinterpreted, xkcd: Duty Calls .
For your question above. Every voltage regulator is manufactured by some company, and they publish what is called a datasheet for the device. That is considered the bible for that device and it will include all the specification, circuit requirements an so on for effectively and safely using that device. A datasheet is to a hardware person what the compiler language standard is for a software person.
Here is a typical datasheet for a popular series of voltage regulators:
The following notes are given following the figure 9 drawing:
(2) CI is required if regulator is located an appreciable distance from power Supply filter.
(3) CO improves stability and transient response.
Lots of hardware design errors can force all the magic smoke out of the electronic devices, that is rather difficult (although not impossible) to do with just software.
Oh, I read data sheets for specific parts I am using. For instance, I found that some of the VRs have a tight range and that using some of the most common 5v ones was right at the edge with a 7.4v LiPo battery. Again, this is what I consider "cookbook circuitry". I don't have the skill set to look at schematics and determine what a circuit does from that. The tables and text in datasheets is what I read.
It is preferable to understand it completely, but I bristle at the statement that you can't use these micro-controllers without reading the schematic.