I was trying to figure out a complicated sketch without progress. I decided to look at something different to take my mind off the problem. I ran the example sketch named in my post title. It looked simple enough, but something weird is going on, and I was wondering if anybody can offer useful comments. The purpose of the example is to write a state change to the serial monitor. The schematic uses an on/off switch connected to pin 2. I used a little push button switch in my parts box. It is supposed to send a 0 or 1 to the serial monitor when pressed and then released. I thought my wiring was wonky because the state change from 0 to 1 was happening before I even pushed the button! Then I noticed all I had to do was put my hand within a couple of inches of the button and the state changed. I pulled the button out of the breadboard and, by putting my hand within a few inches of the pin jumpers only, I changed the state. I guess this is an example of capacitance bouncing for want of a more specific term. How can it be controlled?
Pullup or pulldown resistors to set the pin to the desired steady state. If you desire a high state at rest, you can even use the internal pull-up resistor by setting the pin as such
brianeo:
The schematic uses an on/off switch connected to pin 2.
The schematic of the example does actually have a pulldown resistor.....
Thanks nyphot, Zapro, & tinman13kup. The picture of the schematic fixed the problem. I didn't know the built-in examples had them. I will look for the other examples. I thought a resistor was needed, but didn't know where to put one. The video by baldengineer is instructive. The pullup code mentioned by him, Zapro, and tinman13kup will have the same effect if written into the sketch because the Uno board has a built-in resistor. That coding is missing from the tutorial sketch. Building on what I learned by this, is it correct to assume a resistor should be added to a circuit, or included in the code, when using any sensor? The problem would be false readings. I am aiming at a specific gadget using a PIR sensor.
brianeo:
is it correct to assume a resistor should be added to a circuit, or included in the code, when using any sensor?
No, it should only be used to prevent a pin from floating. Whether that's necessary depends on the sensor.
brianeo:
I am aiming at a specific gadget using a PIR sensor.
Resistor should not be necessary to prevent the pin from floating with a PIR, at least it's not necessary with the common PIR modules I have used.
In other words it must be grounded? A floating pin is an un-grounded pin. If that is correct, it's basic electrical engineering not to leave an electrical wire un-grounded. A resistor prevents a ground wire in a circuit board from shorting by dissipating current. I am still thinking about what a sensor does. I notice they're usually 3-wire things.
brianeo:
A floating pin is an un-grounded pin.
Nope, it's nor grounded nor at Vcc. It's just not connected to any source. It's an antenna.
Look at it as a ball in the back of you car when you're driving a rough road. Is the ball left or right? (Aka, pin HIGH or low?) You can't tell, the car movement (noise aka antenna coupling) moves the ball all over the place. You can glue it in place (connect it HIGH or LOW) but that way you can't move it anymore (if you try to get it to the other side you have a dead short). You can also strap a rubber band between the ball and a side (pull up or pull down resistor). That will keep the ball in place while driving but if you apply force (aka, try to bring the line HIGH or LOW) you can still do so
brianeo:
I am still thinking about what a sensor does.
That completely depends on the sensor...
brianeo:
I am still thinking about what a sensor does. I notice they're usually 3-wire things.
A switch is a sensor, and they're mostly 2 wires.
Those with 3 will likely need their own power supply to enable the workings of what ever happens inside, which would be 2 of the wires, then still only output the info on 1 wire. An LM35 temperature sensor's like that for example, as are Hall effect sensors.
What you do need to know is, what does the output tell you? They can be simple digital high or low where the on or off tells you something, like a switch open or closed. A switch wired with a pullup is so-called active low which means high is open; a pulldown is the opposite way round.
Or it can be digital but a stream of 1s and 0s which need to be read: DHT style temperature and humidity sensors are like that, and usually come with a library to read the data. 3 wires, ie power and data, but not just a simple matter of looking to see if the data line is high or low: there's a message in the stream.
Then some are analog, like a potentiometer or the LM35 mentioned. The voltage on the output is the message, and the LM35 data sheet for example will tell you the relationship between the voltage and the temperature. Since an Arduino can only read voltage, and doesn't know about temperature, you have to code your way round that to calculate the temperature from the read voltage.
Read the sensor data sheet. In many cases if the interpretation of the output seems difficult, someone will already have written an Arduino library.
I'm waiting for a temperature sensor to arrive in the mail. I hope I ordered the right item. Attached is a schematic for a temperature sensor I found after reading your reply. I'm not certain this is the exact part I ordered, but this looks typical of a lot of sensors used with Arduino. Resistor, noted. I want to build a gadget that tells me when food that has just come out of the oven is cool enough to serve and eat. The temperature probe is to be tucked-into the casserole, and when it is at ideal temperature for serving, a buzzer sounds. Meanwhile, I'm with the family. I played with bimetal thermostats for years. They work fine, but calibration is a guess. I am looking forward to writing a sketch that is accurate enough to think about prototyping the gadget.
brianeo:
is it correct to assume a resistor should be added to a circuit, or included in the code, when using any sensor? The problem would be false readings. I am aiming at a specific gadget using a PIR sensor.
No, you can't generalize like that. A push-button switch is sometimes open, ie not physically connected at all,
whereas most sensors are permanently connected to your input pin.
But yes, some sensors will act rather like a push-button - this is called open-collector or open-drain
output, and a pullup (or sometimes a pulldown) resistor may be needed. This way of doing things
can make it easy to inter-work between systems with different voltages, and allows several outputs
to be commoned and act like an implicit logic gate - this is commonly done for "fault" logic outputs,
which are often active low, and open-collector outputs - any output that's active pulls the fault line low,
no other components or logic gates are needed.
When you have a digital bus there are usually outputs that go "high-Z" (ie high impedance, or open circuit),
so that different devices can share the bus - the bus design specifies when each device is allowed to
drive its outputs. For instance SPI, I2C buses work like this.
Thanks for this definition:
A push-button switch is sometimes open, ie not physically connected at all, whereas most sensors are permanently connected to your input pin.
We learn things by induction. I have heard sensors called switches. I have started doing it, myself, but without a solid understanding. While a sensor switches, it is not like an open-or-closed switch, because it is always closed, so to speak, but the wire goes high-or-low, depending on activity. These terms get used sometimes correctly and at others not, and few who know bother to point out the error. About your comments concerning logic, while it is above my present understanding, it suggests to my intuition that a solid state switch, such as a sensor, is both (or neither) open or closed, but connected, awaiting a pulse.
An example of MarkT's point there about not always needing a resistor, is from the Allegro A110X hall sensors. These are 3 wire devices: 2 for its power and 1 output.
From the datasheet:
The output of these devices switches low (turns on) when a magnetic field (south polarity) perpendicular to the Hall sensor exceeds the operate point threshold..... When the magnetic field is reduced below the release point, BRP , the device output goes high (turns off).
So its output is always high or low, provided the sensor itself is powered up, and you don't have to steer it one way or the other to overcome any kind of unknown state. Moral of the story: always read the datasheet.....
(Side note: Did you see it says "switches low (turns on)" which might seem a bit back asswards, where our intuition might tell us that on should be high. That's not the case, there's no reason to think that. And anyway, since we can program our Arduino, we can use an "if....else" and do what we like under both the high and low conditions.)
septillion:
Nope, it's nor grounded nor at Vcc. It's just not connected to any source. It's an antenna.Look at it as a ball in the back of you car when you're driving a rough road. Is the ball left or right? (Aka, pin HIGH or low?) You can't tell, the car movement (noise aka antenna coupling) moves the ball all over the place. You can glue it in place (connect it HIGH or LOW) but that way you can't move it anymore (if you try to get it to the other side you have a dead short). You can also strap a rubber band between the ball and a side (pull up or pull down resistor). That will keep the ball in place while driving but if you apply force (aka, try to bring the line HIGH or LOW) you can still do so
That completely depends on the sensor...
Lol. +1 for this story.