0

The code is as follows:

void loop(){ 

    light = analogRead(5); 
    Serial.println(light); 
    analogWrite(3,light/4); 

}
3

Not really a physics question, but I will answer anyway.

loop() is the standard Arduino function which contains code that the microprocessor runs repeatedly (as opposed to code in setup() which is only run once).

light = analogRead(5); takes an analogue voltage reading from pin 5, encodes it as an integer value (either 10 or 12 bits depending on the Arduino model) and stores the result in the variable light.

Serial.println(light); outputs the value of the variable light to the Arduino's serial port. This is a standard way of outputting data from the Arduino so that it can be displayed on a connected computer.

analogWrite(3, light/4); outputs the value of light/4 as a phase-width modulated (PWM) signal to pin 3. Although Arduino output pins are either on (5 volts) or off (ground) at any point in time, PWM signals allow the Arduino to switch an output pin on and off rapidly so that the average voltage seen on that pin is an intermediate value. The duty cycle of the PWM signal which determines the average voltage on the pin is encoded as an 8 bit integer, so light is probably divided by 4 here to convert it from 10 bits to 8 bits.

So the Arduino will repeatedly read an input voltage from pin 5, output an integer representing this voltage to the serial port, and create a PWM signal on pin 3 that has an average voltage the same as that of the input voltage.

See the Arduino language reference page for more information.

3
  • Great answer. What are all the dollar signs for?
    – CrossRoads
    Mar 17 '20 at 13:02
  • 1
    @CrossRoads, The question and the answer were migrated from physics.stackexchange.com. In that forum, the server will use MathJax to render any text enclosed by dollar signs as mathematical equations. It's not unusual for people on that site to use MathJax even for an "equation" that is just a single, literal constant. Mar 17 '20 at 13:26
  • 1
    To make the answer clearer I have removed the MathJax markup and made other small formatting changes.
    – gandalf61
    Mar 17 '20 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy