Need help with DigitalReadSerial Tutorial

Hi all,

First, if this is the wrong place to post I apologize. I wasn't sure where to put this question.
I am also a complete newb to electronics and all this embedded stuff, so please bear with me.

I am trying to follow the DigitalReadSerial built in example. I am using a Arduino Nano 33 IoT board.
The example has a different board that seems to use 5V instead of the 3.3V my IoT does. And I did see the warnings about having 5V damage the IoT. The example calls for a 10K resistor. I thought I needed to change the resistance because I have 3.3V, so I lowered the resistance to 6K. Ohm's law seemed to show needing 6.6k but I was hoping 6K would be close enough.

Anyway, when I run the example, when the switch button is not pressed the serial monitor shows alternating 1's and 0's. When I press the switch button down I get steady 1's. So yay, half a success. (I will take it lol)

Also, when I run the "Button" built in example, which seems to be the exact same setup, I get a dim LED when not pushing the switch button and a brighter LED when I press the button. So, it seems similar.

I am wondering why the output fluctuates on the Low side? Any thoughts?
Maybe I need to add the extra .6k ohms? Maybe I should have kept the 10K?
I have seen some posts about connecting to TX or RX and I am not doing that. I have the switch connected to pin 2 and the other side of the switch to the resistors to ground.
I have power coming from the 3.3V pin next to the pin13 LED pin.

Any help you experts can give would be greatly appreciated.

You wiring description is hard for me to follow. Can you draw a schematic? It sounds like the switch input is floating when the switch is open (not pressed).

Despite all the tutorials that show switches wired to Vcc and an input, they are not showing the right way to wire a switch. The switch should have one side to ground and the other side to an input with the pinMode set to INPUT_PULLUP. The input will read high when the switch is not pressed and low when the switch is pressed.

The value of external pullup or pulldown resistors is not critical. 10K is a usual value for 5V or 3.3V. Lower values can be used if the switch is a ways away from the processor. Minimum value of 1K or so.

Modified digitalReadSerial example to use switch wired to ground and internal pullup.

// digitalReadSerial exmple modified for the 21st century by groundfungus

// digital pin 2 has a pushbutton attached to it. Give it a name:
const byte pushButton = 2;

// the setup routine runs once when you press reset:
void setup()
{
   // initialize serial communication at 9600 bits per second:
   Serial.begin(9600);
   Serial.println("digitalReadSerial example");
   // make the pushbutton's pin an input, enable internal pullup:
   pinMode(pushButton, INPUT_PULLUP);  
}

// the loop routine runs over and over again forever:
void loop()
{
   // I hate delay.  See the blink without delay example for explanation of millis() for timing.
   static unsigned long timer = 0;
   unsigned long interval = 100;
   if (millis() - timer >= interval)
   {
      timer = millis();
      // read the input pin:
      int buttonState = digitalRead(pushButton);
      // print out the state of the button:
      Serial.println(buttonState);
      //delay(1);       I HATE delay()
   }
}

Switch wiring:

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled “How to use this Forum”.
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

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

A 10K will work just a well with 3V3 logic as with 5V logic.
It sounds like you do not have the resistor connected between gnd and the digital input pin, and the button between 3V3 and the digital input pin.

Thanks… Tom… :slight_smile:

For those wanting to see my code and schematic, I am literally doing the DigitalReadSerial example from this site. The one that comes with the IDE.

This thing:

@groundFungus Your code is almost exactly the same as the tutorial except for the timer you added.
Is this something they just forgot to add to the tutorial?
But your drawing says input is low when pressed. As I understand the tutorial it is supposed to be high when pressed?

The tutorial says this bit:

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it doesn't have a solid connection to voltage or ground, and it will randomly return either HIGH or LOW. That's why you need a pull-down resistor in the circuit.

There is no LED in this tutorial, so I am confused as to what they mean. But maybe I am just having a problem with the resistors.
I will try what you guys have suggested and report my findings.

And thank you all for the responses.

I changed the tutorial code to use a switch wired to ground and the internal pullup.

Despite all the tutorials that show switches wired to Vcc and an input, they are not showing the right way to wire a switch. The switch should have one side to ground and the other side to an input with the pinMode set to INPUT_PULLUP. The input will read high when the switch is not pressed and low when the switch is pressed.

Modified digitalReadSerial example to use switch wired to ground and internal pullup.

I am trying to tell you a better way to use the switch than the way in the tutorial. The tutorial does not reflect the way that things are really done.

I tried your setup groundFungus and it works.

Now what do I do? I can't exactly expect you to post the correct way to do all of the tutorials. This kind of think really irks me. I paid good money for this product and their tutorials are crap? grrrrr
Yeah it was only $20 but still.

Try to go thru as many examples as you can to gain some experience.

There are examples that come with the IDE.

Look at some Youtube videos on the Arduino.

When you run into problems you can ask for help here, lots of volunteers ready to help.

FYI

Arduino links of interest.

How to use this forum:
https://forum.arduino.cc/index.php?topic=149014.0

Listing of downloadable 'Arduino PDFs' :
Either Google >>>- - - - > arduino filetype: pdf
Or
https://www.google.ca/search?q=arduino+filetype%3A+pdf&rlz=1C9BKJA_enCA739CA739&oq=arduino+filetype%3A+pdf&aqs=chrome..69i57j69i65.1385j0j7&hl=en-US&sourceid=chrome-mobile&ie=UTF-8

Listing of downloadable 'C++ PDFs' :
Either Google >>>- - - - > C++ filetype: pdf
Or
https://www.google.ca/search?q=c%2B%2B+filetype%3A+pdf&rlz=1C9BKJA_enCA739CA739&oq=c%2B%2B+filetype%3A+pdf&aqs=chrome..69i57.22790j0j7&hl=en-US&sourceid=chrome-mobile&ie=UTF-8

Arduino cheat sheet:

Watch these:
Arduino programming syntax:

Arduino arithmetic operators:

Arduino control flow:

Arduino data types:

Understanding Destructive LC Voltage Spikes:

OR

Why MOSFET gate resistors:

Some things to read

LCD information:

OR

Reading a schematic:
https://learn.sparkfun.com/tutorials/how-to-read-a-schematic

Language Reference:
https://www.arduino.cc/en/Reference/HomePage

Foundations:

How and Why to avoid delay():
http://playground.arduino.cc/Code/AvoidDelay

Demonstration code for several things at the same time.
http://forum.arduino.cc/index.php?topic=223286.0

Multitasking:
Part 1:

Part 2:

Part 3:

Sparkfun Tutorials:
https://learn.sparkfun.com/tutorials?page=all

Micro Controllers:

Useful links:

Arduino programming traps, tips and style guide:
http://www.gammon.com.au/forum/?id=12153

Call for useful programming discussions

Jeremy Blume:

Arduino products:
https://www.arduino.cc/en/Main/Products

Motors/MOSFETs
http://www.gammon.com.au/motors

Making a library
https://www.arduino.cc/en/Hacking/libraryTutorial

Switches:
http://www.gammon.com.au/forum/?id=11955

Share tips you have come across, 700+ posts:

Debug discussion:

Frequently Asked Questions:
https://www.arduino.cc/en/main/FAQ#toc10

Number 'type's.

  • boolean (8 bit) - simple logical true/false, Arduino does not use single bits for bool
  • byte (8 bit) - unsigned number from 0 to 255
  • char (8 bit) - signed number from -128 to 127. The compiler will attempt to interpret this data type as a character in some circumstances, which may yield unexpected results
  • unsigned char (8 bit) - same as 'byte'; if this is what you're after, you should use 'byte' instead, for reasons of clarity
  • word (16 bit) - unsigned number from 0 to 65535
  • unsigned int (16 bit)- the same as 'word'. Use 'word' instead for clarity and brevity
  • int (16 bit) - signed number from -32768 to 32767. This is most commonly what you see used for general purpose variables in Arduino example code provided with the IDE
  • unsigned long (32 bit) - unsigned number from 0 to 4,294,967,295. The most common usage of this is to store the result of the millis() function, which returns the number of milliseconds the current code has been running
  • long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647
    float (32 bit) - signed number from -3.4028235E38 to 3.4028235E38. Floating point on the Arduino is not native; the compiler has to jump through hoops to make it work. If you can avoid it, you should. We'll touch on this later. Sparkfun.

You select the 'type' best suited for your variables.

ex:

  • your variable does not change and it defines a pin on the Arduino. const byte limitSwitchPin = 34;
  • since an analog variable can be 0 to 1023, a byte will not do, you can select 'int'. int temperature;
  • if your variable needs to be within -64 to +64 a 'char' will do nicely. char joystick;
  • if your variable is used for ASCII then you need type 'char', char myText = {"Raspberry Pie Smells"};
  • if your variable enables some code then boolean can be used. boolean enableFlag = false;
  • millis() returns the time in ms since rebooting, unsigned long currentTime = millis();
    etc.

Oh, and have fun too :slight_smile: !