I mean what it does, why it is needed, what if we don't use it, similarly for Serial.println(sensorValue).
2 Answers
As the name suggests, serial.begin(9600)
is a command you give to the Arduino to begin serial communication. I'm assuming you're a beginner. Anyway, as you might've seen on the IDE, there's a serial monitor. It outputs data that you configure it to output. It won't work unless you begin the serial communication using. The serial.begin(x);
command. X is a variable baudrate, and I highly suggest going with 9600.
Okay, also, serial.println(sensorValue)
is a command that the program gives to the Arduino to print the variable sensorValue
to the serial monitor. Keep in mind that you can also print this through a Bluetooth module by connecting the tx to the rx and the rx to the tx pins. Hope this helped! :)
Serial.begin(9600);
configures the UART interface and sets it to the right baud rate. If you don't to that you can't use the UART interface.
Serial.println(sensorValue);
outputs the sensor value to the UART. If you don't use that you won't see the value of the sensor on the UART.
If you don't want to use the UART then don't use it. If you do want to use the UART then you must use the commands that control the UART.
Serial.begin()
initializes serial-port hardware and data structures so you can use the serial port. See Serial.begin and Serial.println doc pages at arduino.cc for details, and see the answers to What is Serial.begin(9600)? for more background.