Training and testing for new users.

The following posts challenge a new user’s knowledge of Arduino coding and basic electronics.

Arduino questions with answers, based on an Arduino UNO:

  1. When you are writing a sketch in the Arduino IDE, what happens to your sketch when you press Ctrl+T, Cmd+T? Formats the code to make it more easily read.
  2. If weird characters appear on the IDE serial monitor, what is likely wrong? You may need to match monitor baud rate to sketch baud rate.
  3. What is a function? It is like a subroutine in BASIC, you can ‘call’ it from sections within the sketch.
  4. What is the difference between global and local variables? Global, access from anywhere, Local, access from with the module.
  5. Where are global variables defined? Outside of functions, normally at the top of the sketch.
  6. What does const tell the compiler in this line of code? const int myVariable = 13; Variable will not change i.e. it is a constant, takes no SRAM.
  7. What is wrong with this line of code? #define counter = 0; NO = and NO ;
  8. What does /* and */ do to the lines of code that are written between these character pairs? Comments them out.
  9. What does this line of code do? return true; The calling code gets a ‘true’ returned to it.
  10. If a variable has type float, how may bytes are set aside for the variable. float foo = 0; 4
  11. What does byte do in this line of code? byte myFunction(int number) { } There is a returned variable, it is type byte.
  12. Why is it important to use the smallest necessary type when you define your variables? Save SRAM
  13. Why is it not recommended to use the delay() function in your sketches? delay() blocks sketch from running until the delay time is up.
  14. What does this line of code do? counter++; counter is incremented by 1
  15. What is SRAM? Read write memory, where changeable/dynamic variables are.
  16. What does the bootloader do? Loads the compiled code to static memory.
  17. What two ways can you restart your Arduino? Power off and push the reset button
  18. What does the millis() function return when it is called? Current run time in milliseconds since last reset, it resets to 0 at ~49 days and starts incrementing again.
  19. What is the range of numbers you can store in a variable of type byte? 0 - 255
  20. What is the index of the letter F in this array? char textMessage = “Have Fun with Arduino”; 5, zero relative.
  21. How many elements are in the above array? 22 including the null terminating character \0
  22. How many iterations are we setting up in this line of code?
    for( byte counter = 1; counter < 10; counter++ ); 9
  23. Is there anything wrong with this line of code, if yes, what?
    if(myVar = true) Serial.println(“The light is ON”); = should be ==
  24. What is the difference in these two lines of code? Serial.println(“Test”); Serial.print(“Test”); println gives a CR, print does not.
  25. What is the purpose of && in this line of code? if( foo >= 100 && foo <= 200); ‘and’, both conditions must be met.
  26. Why is it important to comment your lines of code? So you can ‘easily’ understand what is happening in your code.
  27. What does the word static do in this line of code? static int temperature = 0; The variable’s value is retained the next time it is accessed.
  28. This error is displayed after compiling: ‘temp’ was not declared in this scope. Why? temp is only available inside the for loop.
    for(byte temp = 0; temp <=200; temp = temp +10) { Serial.println(temp); }
    byte myVar = temp;
  29. What is the value of foo when these lines of code are executed? foo = 12; foo = foo % 10; 2
  30. Is there anything wrong with this line of code, if yes, what?
    for( char counter = -10; counter < 10; counter++) It is okay, char # range is -128 to 127
  31. If the A0 pin is connected to 2.5 volts, what is the value of foo? unsigned int foo = analogRead(A0); 2.5V is ½ of 5V therefore since the range being 0-1023, value is 512/511
  32. What is the range of numbers that can be used for num in this line of code. analogWrite(13, num); Can be 0-255
  33. What does this line of code do? temp = !temp; compliments ‘temp’, if temp was HIGH it is now LOW vv
  34. What does INPUT_PULLUP do in this line of code? pinMode(2, INPUT_PULLUP); Turns on the internal pullup resistor ~20-50K
  35. What does the map() function do? A function that maps/converts a value from one range to another range.
  36. If you are told your code is blocking, what does this mean? Code execution slows/stops, makes the sketch unresponsive.
  37. What does a pull down resistor do to a floating input pin? Ensures a logic LOW when the pin is hi Z
  38. What does this line of code do? tone(4, 750, 500); A 750 Hz tone on pin 4 for ½ second
  39. What is the value of foo after this line is executed? unsigned long foo = 60601000UL; 3,600,000 one hour
  40. Describe what is happening in this line of code. Serial.println (F(“Program starting.”));
    The string is stored in static memory, not SRAM.
  41. Why should variables, accessed outside in an interrupt service routine and modified in the ISR, be given the ‘volatile’ qualifier? Making them volatile makes them reside in RAM. This prevents them from being placed in a ‘Storage Register’ where it could be inadvertently changed.
  42. What do the functions noInterrupts( ) and interrupts( ) do? ‘Disables’ interrupts and ‘Enables’ interrupts. Prevents code between these two functions from being interrupted.

A user has connected the circuits below.
What is wrong with the circuits connected to the following pins? D2, D3, D4 and D5
D2 The LED needs a current limiting resistor ~220R
D3 5 volts will not turn on 3 series LEDs
D4 Switch needs a pulldown resistor OR connect the top of switch to GND and use INPUT_PULLUP.
D5 LED is shorted out the way it is shown.

2 Breadboard2.jpg

Another User has connected a different circuit.
They cannot get the LED to flash, what is wrong with their circuit?
The power rails are separated in the middle, need to place jumpers to connect LHS to RHS.

2.5 Breadboard.jpg

A user has wired up an Arduino shield in the image below.

Which circuit below matches the wiring? A, B, C or NONE B

3 PointToPoint WhichCircuit.jpg

3 PointToPointQuestion.jpg

Referring to the circuit below, when D2 is LOW, what will the voltmeters read?

Assume Vf for a red LED = 2V therefore “A” will be ~1.5V “B” will be -1.5V and “C” will be 2V.

4 Voltmeter.jpg

  1. Identify what these components are and what function they serve:
    Designation Component Name What is its Function
    R17 LDR Light causes a resistance change (detects light intensity)
    R19 Resistor Input pull down resistor
    R16 Resistor LED current limiting resistor
    R14 Resistor Limits the pin D2 current while the gate capacitance charges
    D10 Diode Kickback protection for the MOS FET
    D9 LED Used as an indicator light
    K1 Relay Provides isolation from the drive cct. Mechanical contacts. . .
    Q4 N-MOS FET Provides drive current path to energize the realy
  2. After this line of code executes, digitalWrite( 3 , HIGH ); will D8 be on or off? OFF
  3. Do you have to send a HIGH or a LOW to pin D4 to turn on D9? HIGH
  4. analogWrite(3 , 127); works, but analogWrite(4 , 127); doesn’t work. Why is this? Pin 4 is not for PWM.
  5. What must the voltage rating of K1 be? 12V
  6. If R17 is 20K what is the voltage at A0 on the UNO? Rt = 30K therefore, 5V/3 = ~1.67V
  7. If Q4 is turned on, what is the resistance between pin 1 and 3 of K1? Zero ohms

5 WhatIsTheFunction.jpg

A new user is trying to get a project working. They have connected their circuit up exactly as seen in the schematic below. Nothing works as it should.
Every time they plug in a new fuse into the motor circuit, the fuse burns out.
D6 is installed backwards, the drain and source on the MOS FET are swapped.
The LED does not turn on when the switch is pressed.
D7 is backwards. Also, we need a pulldown resistor from D9 to GND, ~10K.
The IDE serial monitor is supposed to show what voltage is on input A0. The serial monitor displays a varying value without adjusting the pot. You need to connect the battery negative to the Arduino GND.
What is wrong with these circuits?

6 WhatIsWrong.jpg

For the image below, write a line of code to:

• read the voltage on A0.
int myVar = analogRead(A0);

• send the reading from A0 to the LED D5.
analogWrite(9 , myVar/4);

• turn the LED D4 off.
digitalWrite(10 , HIGH);

7 Write Code.jpg

//**************************************************************
//
// Add comments to this sketch so a reader can easily follow what is happening.
// These comments must be more than just repeating what the line of code is doing.
//
//**************************************************************

//
#define LEDon  LOW
#define LEDoff HIGH


//**************************************************************

const byte redLED    = 7;
const byte greenLED  = 10;
const byte yellowLED = 13;
const byte Outputs[] = {7, 10, 13};

byte State;

const unsigned long redInterval    = 10000;
const unsigned long yellowInterval = 5000;
const unsigned long greenInterval  = 10000;

unsigned long LEDmillis;

//**************************************************************
void setup()
{
  for (byte x = 0; x < sizeof(Outputs); x++)
  {
    pinMode(Outputs[x], OUTPUT);
    digitalWrite(Outputs[x], LEDoff);
  }

  digitalWrite(redLED, LEDon);
  
  LEDmillis = millis();
  
  State = 0;   

  }

//**************************************************************
void loop()
{
  checkLEDs();
}

//**************************************************************
void checkLEDs()
{
  switch (State)
  {
    //**************************
    case 0:
      if (millis() - LEDmillis >= redInterval)
      {
        digitalWrite(redLED, LEDoff);
        digitalWrite(greenLED, LEDon);
        digitalWrite(yellowLED, LEDoff);

        State = 1;
        LEDmillis = millis();
      }
      break;

    //**************************
    case 1:
      if (millis() - LEDmillis >= greenInterval)
      {
        digitalWrite(redLED, LEDoff);
        digitalWrite(greenLED, LEDoff);
        digitalWrite(yellowLED, LEDon);

        State = 2;
        LEDmillis = millis();
      }
      break;

    //**************************
    case 2:
      if (millis() - LEDmillis >= yellowInterval)
      {
        digitalWrite(redLED, LEDon);
        digitalWrite(greenLED, LEDoff);
        digitalWrite(yellowLED, LEDoff);

        State = 0;
        LEDmillis = millis();
      }
      break;
  }
}

//**************************************************************
//What does this sketch do?

const int ledPin          = 13;
const int potentiometer   = A0;
unsigned long startTime;

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  int rate = analogRead(potentiometer);
  rate = map(rate, 0, 1023, 5000, 100);
  checkTime(rate);
}

void checkTime(int interval)
{
  static byte LEDstate = HIGH;
  if (millis() - startTime >= interval)
  {
    startTime = millis();
    digitalWrite(ledPin, LEDstate);
    LEDstate = !LEDstate;
  }
}

Future

Wow. Talk about throwing us in the deep end.
As a hardware technician just getting started with an Uno, 95% of those questions are Greek to me.
Also, the inline images of the circuit diagrams are missing in replies 2 through 6.

Regards,
David M.

Images are included and should be visible in your browser.
If you cannot see them, try a different browser.

All answers are in beige ink on white, included except post #7 and 8.

PLEASE DONT POST YOUR ANSWERS WITHIN THIS THREAD !

It will be better if you discuss the problems offline, or in a new, different thread - so this topic remains ‘clean’, and doesn’t confuse other newbies with incorrect ‘guesses’ that mislead others.

Nice work LarryD

  1. What does /* and *\ do to the lines of code that are written between these character pairs?

Is that a typo, or a trick question?

(Ctrl-t doesn’t do anything on MY Mac)

Typo, now corrected.

What’s a Mac :wink:
Changed: Ctrl+T, Cmd+T,

Arduino questions

To be more useful online (or in general), you (we?) should design a set of "multiple choice" answers for each question. A good set of multiple choice answers (which are harder to design than you might think.)
For additional usefulness, figure out how to randomize questions/answers so that if you take the test again, the answers will be different.
(Hmm. Does anyone know of sites that let you generate and provide "online quizzes"? I've seen some good quizzes on EdX and Coursera a similar, bit I'm pretty sure they don't allow just anyone to generate classes...)

ElectroDFW:
As a hardware technician just getting started with an Uno, 95% of those questions are Greek to me.

37. What does a pull down resistor do to a floating input pin?

ddq.png
Figure-1: Explaining the importance of internal pull-up, external pull-up, and external pull-down.

Consider bit-2 IO line of Port-D Register. When this IO line is configured to work as an input line, it takes over the symbolic name -- PIND2. The external button (K1) can be connected with this input line via DPin-2 (Digital Pin Connector) of Arduino UNO.

In Fig-1, there are there are three resistors (R1, R2, and Rp) around DPin-2. R1 is an external pull-down resistor placed by the user; R2 is an external pull-up resistor placed by the user; Rp is an internal pull-up resistor placed by the designer of the ATmega329P Microcontroller. The resistors are associated with switches by which they could be connected or disconnected.

When all the switches are at opened condition, the input line assumes no definite voltage level. The voltage levle of DPin-2 could be 0V or 5V or in between or all the time changing. This is known as floating condition of the input line.

The insertion of the external pull-down resistor (R1) will propagate 0V from the GND-pin onto DPin-2; now, the DPin-2 is no more at the floating state. When button K1 is closed, the DPin-2 will assume LH state.

The insertion of the external pull-up resistor (R2) will propagate 5V from the 5V-pin onto DPin-2; now, the DPin-2 is no more at the floating state. At this condition, the 5V terminal of button K1 has to be connected to GND. When button K1 is closed, the DPin-2 will assume LL state.

The insertion of the internal pull-up resistor (Rp) will propagate 5V from the 5V-pin onto DPin-2; now, the DPin-2 is no more at the floating state. At this condition, the 5V terminal of button K1 has to be connected to GND. When button K1 is closed, the DPin-2 will assume LL state.

34. What does INPUT_PULLUP do in this line of code? pinMode(2, INPUT_PULLUP);
The execution of the pinMode(2, INPUT_PULLUP); function configures DPin-2 to work as input line along with the internal pull-up resistor connected. If we don't to connect the internal pull-up (sometimes, we don't need it because of very high value; it does not work for me with external interrupt lines), we execute the instruction pinMode(2, INPUT);. In this case, we have to connect the external pull-up or pull-down as per requirement/convenience to avoid floating condition of the input line.

ddq.png

15. What is SRAM? Read write memory, where changeable/dynamic variables are.
Static Random Access Read Write Memory, [...].

17. What two ways can you restart your Arduino?
When we say restart -- it specifically refers to 'again start when it is already in running condition' similar to a 're-trigger-able one-shot. The Arduino UNO can be restarted in two ways:

(a) by pressing/releasing the on-board RESET switch
(b) by re-invoking the Serial Monitor.

31. If the A0 pin is connected to 2.5 volts, what is the value of foo? unsigned int foo = analogRead(A0);

The variable foo will contain the bit pattern : 0000000111111111 (0x01FF)


Figure-1: Internal structure of ADC of ATmega328P Microcontroller

The analogRead(A0); command selects Ch-0 of the ADC and connects the 2.50V signal to the input of the 10-bit uni-polar ADC. After conversion, the 10-bit binary value is stored inside ADCL and ADCH Registers from which the value enters into user defined variable foo.

How does the value come up to: 000000111111111?

When the input to the ADC is equal to the VREF (the 5V -- called Full Scale value), the ADC value is all 1s (1111111111 = 1023)

When the input to the ADC is equal to VREF/2 (2.50V), the ADC value is (1023/5)*2.50 = 511 = 0111111111.

In Fig-1, we see that the upper 6-bit of the ADCH Register is always 0s; as a result, the value that enters into the variable foo is: 000000 0111111111 = 0x01FF.

Thise mentioned are ‘HARDWARE resets’ - there are several additional methods that will ‘reset’ your AVR microcontroller chip - all are just as important - as they often catch beginners unexpectedly.

AVR reset sources

Capture.JPG

Possibly the most interesting is the Watchdog reset - read up on it…!