Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Arduino
Buy | Download | Getting Started | Learning | Reference | Hardware | FAQ
Blog » | Forum » | Playground »
Serial
begin()
Description
Sets the data rate in bits per second (baud) for serial data transmission. For communicating with the computer, use one of these rates: 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. You can, however, specify other rates - for example, to communicate over pins 0 and 1 with a component that requires a particular baud rate.
Syntax
Serial.begin(speed)
Arduino Mega only:
Serial1.begin(speed)
Serial2.begin(speed)
Serial3.begin(speed)
Parameters
speed: in bits per second (baud) - long
Returns
nothing
Example:
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {}
Arduino Mega example:
// Arduino Mega using all four of its Serial ports
// (Serial, Serial1, Serial2, Serial3),
// with different baud rates:
void setup(){
Serial.begin(9600);
Serial1.begin(38400);
Serial2.begin(19200);
Serial3.begin(4800);
Serial.println("Hello Computer");
Serial1.println("Hello Serial 1");
Serial2.println("Hello Serial 2");
Serial3.println("Hello Serial 3");
}
void loop() {}
Thanks to Jeff Gray for the mega example
See also
* Serial.end()
Reference Home
Share|
Edit Page | Page History | Printable View | All Recent Site Changes
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis()/1000);
}
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int val = 0;
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
void loop()
{
val = analogRead(0)/4.011764;
if (val > 252)
{
selectLineOne();
delay(50);
lcd.print("It is very dark ");
selectLineTwo();
delay(50);
lcd.print("in this room.");
delay(100);
}
if (val > 246 && val < 252)
{
selectLineOne();
delay(50);
lcd.print("It is pretty ");
selectLineTwo();
delay(50);
lcd.print("dark here. ");
delay(100);
}
if (val > 235 && val < 246)
{
selectLineOne();
delay(50);
lcd.print("It is pretty ");
selectLineTwo();
delay(50);
lcd.print("bright here. ");
delay(100);
}
if (val < 235)
{
selectLineOne();
delay(50);
lcd.print("It is bright ");
selectLineTwo();
delay(50);
lcd.print("in this room. ");
delay(100);
}
}
void selectLineOne(){ //puts the cursor at line 0 char 0.
lcd.print(0xFE, BYTE); //command flag
lcd.print(128, BYTE); //position
}
void selectLineTwo(){ //puts the cursor at line 0 char 0.
lcd.print(0xFE, BYTE); //command flag
lcd.print(192, BYTE); //position
}
void clearLCD(){
lcd.print(0xFE, BYTE); //command flag
lcd.print(0x01, BYTE); //clear command.
}
void backlightOn(){ //turns on the backlight
lcd.print(0x7C, BYTE); //command flag for backlight stuff
lcd.print(157, BYTE); //light level.
}
void backlightOff(){ //turns off the backlight
lcd.print(0x7C, BYTE); //command flag for backlight stuff
lcd.print(128, BYTE); //light level for off.
}
void serCommand(){ //a general function to call the command flag for issuing all other commands
lcd.print(0xFE, BYTE);
}
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int val = 0;
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
void loop()
{
val = analogRead(0)/4.011764;
if (val > 252)
{
selectLineOne();
delay(50);
lcd.print("It is very dark ");
selectLineTwo();
delay(50);
lcd.print("in this room.");
delay(100);
}
if (val > 246 && val < 252)
{
selectLineOne();
delay(50);
lcd.print("It is pretty ");
selectLineTwo();
delay(50);
lcd.print("dark here. ");
delay(100);
}
if (val > 235 && val < 246)
{
selectLineOne();
delay(50);
lcd.print("It is pretty ");
selectLineTwo();
delay(50);
lcd.print("bright here. ");
delay(100);
}
if (val < 235)
{
selectLineOne();
delay(50);
lcd.print("It is bright ");
selectLineTwo();
delay(50);
lcd.print("in this room. ");
delay(100);
}
}
void selectLineOne(){ //puts the cursor at line 0 char 0.
lcd.print(0xFE, BYTE); //command flag
lcd.print(128, BYTE); //position
}
void selectLineTwo(){ //puts the cursor at line 0 char 0.
lcd.print(0xFE, BYTE); //command flag
lcd.print(192, BYTE); //position
}
void clearLCD(){
lcd.print(0xFE, BYTE); //command flag
lcd.print(0x01, BYTE); //clear command.
}
void backlightOn(){ //turns on the backlight
lcd.print(0x7C, BYTE); //command flag for backlight stuff
lcd.print(157, BYTE); //light level.
}
void backlightOff(){ //turns off the backlight
lcd.print(0x7C, BYTE); //command flag for backlight stuff
lcd.print(128, BYTE); //light level for off.
}
void serCommand(){ //a general function to call the command flag for issuing all other commands
lcd.print(0xFE, BYTE);
}
int photocellPin0 = 0; // the cell and 10K pulldown are connected to a0
int photocellReading0; // the analog reading from the analog resistor divider
float Res0=10.0; // Resistance in the circuit of sensor 0 (KOhms)
// depending of the Resistance used, you could measure better at dark or at bright conditions.
// you could use a double circuit (using other LDR connected to analog pin 1) to have fun testing the sensors.
// Change the value of Res0 depending of what you use in the circuit
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int val = 0;
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16,1);
}
void loop(void) {
photocellReading0 = analogRead(photocellPin0); // Read the analogue pin
float Vout0=photocellReading0*0.0048828125; // calculate the voltage
int lux0=104.1*Vout0; // calculate the Lux
// Print the measurement (in Lux units) in the screen
lcd.print(" Lux\t");
lcd.print(lux0); // Print the measured level at pin 0
delay(100);
// turn off automatic scrolling
lcd.noAutoscroll();
}