You're also going to need to 2 Arduino CAN Bus Modules. These can be. We will then display the received output in our serial monitor. We will create. I'm trying to send data from a Nano v3.0 to a DUE. The communication is one way only. I know about the 5v vs 3v3 issue, so i bought this.
Master Reader/Slave Sender
In some situations, it can be helpful to set up two (or more!) Arduino and Genuino boards to share information with each other. In this example, two boards are programmed to communicate with one another in a Master Reader/Slave Sender configuration via the I2C synchronous serial protocol. Several functions of Arduino's Wire Library are used to accomplish this. Arduino 1, the Master, is programmed to request, and then read, 6 bytes of data sent from the uniquely addressed Slave Arduino. Once that message is received, it can then be viewed in the Arduino Software (IDE) serial monitor window.
The I2C protocol involves using two lines to send and receive data: a serial clock pin (SCL) that the Arduino or Genuino Master board pulses at a regular interval, and a serial data pin (SDA) over which data is sent between the two devices. As the clock line changes from low to high (known as the rising edge of the clock pulse), a single bit of information - that will form in sequence the address of a specific device and a a command or data - is transferred from the board to the I2C device over the SDA line. When this information is sent - bit after bit -, the called upon device executes the request and transmits it's data back - if required - to the board over the same line using the clock signal still generated by the Master on SCL as timing.
Because the I2C protocol allows for each enabled device to have it's own unique address, and as both master and slave devices to take turns communicating over a single line, it is possible for your Arduino or Genuino board to communicate (in turn) with many devices, or other boards, while using just two pins of your microcontroller.
Hardware Required
- 2 Arduino or Genuino Boards
- hook-up wires
Circuit
Connect pin 4 (the data, or SDA, pin) and pin 5 (the clock, or SCL, pin) on the master board to their counterparts on the slave board. Make sure that both boards share a common ground. In order to enable serial communication, the master board must be connected to your computer via USB.
If powering the boards independently is an issue, connect the 5V output of the Master to the VIN pin on the slave.
image developed using Fritzing. For more circuit examples, see the Fritzing project page
Schematic
Code
Code for Master Reader - Program for Arduino 1
// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the 'Wire Slave Sender' example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
voidsetup(){
Wire.begin();// join i2c bus (address optional for master)
Serial.begin(9600);// start serial for output
}
voidloop(){
Wire.requestFrom(8,6);// request 6 bytes from slave device #8
while(Wire.available()){// slave may send less than requested
char c = Wire.read();// receive a byte as character
Serial.print(c);// print the character
}
delay(500);
}
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the 'Wire Slave Sender' example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
voidsetup(){
Wire.begin();// join i2c bus (address optional for master)
Serial.begin(9600);// start serial for output
}
voidloop(){
Wire.requestFrom(8,6);// request 6 bytes from slave device #8
while(Wire.available()){// slave may send less than requested
char c = Wire.read();// receive a byte as character
Serial.print(c);// print the character
}
delay(500);
}
Code for Slave Sender - Program for Arduino 2
// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the 'Wire Master Reader' example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
voidsetup(){
Wire.begin(8);// join i2c bus with address #8
Wire.onRequest(requestEvent);// register event
}
voidloop(){
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent(){
Wire.write('hello ');// respond with message of 6 bytes
// as expected by master
}
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the 'Wire Master Reader' example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
voidsetup(){
Wire.begin(8);// join i2c bus with address #8
Wire.onRequest(requestEvent);// register event
}
voidloop(){
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent(){
Wire.write('hello ');// respond with message of 6 bytes
// as expected by master
}
See Also
- Wire.begin()
- Wire.RequestFrom()
- Wire.receive()
- Wire.send()
- Wire.onRequest()
- Wire Library – Your reference for the Wire Library.
- DigitalPotentiometer - How to control an Analog Devices AD5171 Digital Potentiometer.
- SFRRanger_reader - How to read an ultra-sonic range finder interfaced via the I2C.
- Master Writer/Slave receiver - Two Arduino are programmed to communicate in a Master Writer/Slave Receiver configuration via the I2C.
Last revision 2018/05/17 by SM
Switch (case) Statement, used with serial input
An if statement allows you to choose between two discrete options, TRUE or FALSE. When there are more than two options, you can use multiple if statements, or you can use the switch statement. Switch allows you to choose between several discrete options.
This tutorial shows you how to use switch to turn on one of several different LEDs based on a byte of data received serially. The sketch listens for serial input, and turns on a different LED for the characters a, b, c, d, or e.
Hardware Required
- Arduino or Genuino Board
- 5 LEDs
- 5 220 ohm resistors
- hook-up wires
- breadboard
Circuit
Five LEDs are attached to digital pins 2, 3, 4, 5, and 6 in series through 220 ohm resistors.
To make this sketch work, your board must be connected to your computer. In the Arduino IDE open the serial monitor and send the characters a, b, c, d, or e to lit up the corresponding LED, or anything else to switch them off.
click the image to enlarge
image developed using Fritzing. For more circuit examples, see the Fritzing project page
Schematic
click the image to enlarge
Code
/*
Switch statement with serial input
Demonstrates the use of a switch statement. The switch statement allows you
to choose from among a set of discrete values of a variable. It's like a
series of if statements.
To see this sketch in action, open the Serial monitor and send any character.
The characters a, b, c, d, and e, will turn on LEDs. Any other character will
turn the LEDs off.
The circuit:
- five LEDs attached to digital pins 2 through 6 through 220 ohm resistors
created 1 Jul 2009
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/SwitchCase2
*/
voidsetup(){
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pins:
for(int thisPin =2; thisPin <7; thisPin++){
pinMode(thisPin,OUTPUT);
}
}
voidloop(){
// read the sensor:
if(Serial.available()>0){
int inByte =Serial.read();
// do something different depending on the character received.
// The switch statement expects single number values for each case; in this
// example, though, you're using single quotes to tell the controller to get
// the ASCII value for the character. For example 'a' = 97, 'b' = 98,
// and so forth:
switch(inByte){
case'a':
digitalWrite(2,HIGH);
break;
case'b':
digitalWrite(3,HIGH);
break;
case'c':
digitalWrite(4,HIGH);
break;
case'd':
digitalWrite(5,HIGH);
break;
case'e':
digitalWrite(6,HIGH);
break;
default:
// turn all the LEDs off:
for(int thisPin =2; thisPin <7; thisPin++){
digitalWrite(thisPin,LOW);
}
}
}
}
Switch statement with serial input
Demonstrates the use of a switch statement. The switch statement allows you
to choose from among a set of discrete values of a variable. It's like a
series of if statements.
To see this sketch in action, open the Serial monitor and send any character.
The characters a, b, c, d, and e, will turn on LEDs. Any other character will
turn the LEDs off.
The circuit:
- five LEDs attached to digital pins 2 through 6 through 220 ohm resistors
created 1 Jul 2009
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/SwitchCase2
*/
voidsetup(){
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pins:
for(int thisPin =2; thisPin <7; thisPin++){
pinMode(thisPin,OUTPUT);
}
}
voidloop(){
// read the sensor:
if(Serial.available()>0){
int inByte =Serial.read();
// do something different depending on the character received.
// The switch statement expects single number values for each case; in this
// example, though, you're using single quotes to tell the controller to get
// the ASCII value for the character. For example 'a' = 97, 'b' = 98,
// and so forth:
switch(inByte){
case'a':
digitalWrite(2,HIGH);
break;
case'b':
digitalWrite(3,HIGH);
break;
case'c':
digitalWrite(4,HIGH);
break;
case'd':
digitalWrite(5,HIGH);
break;
case'e':
digitalWrite(6,HIGH);
break;
default:
// turn all the LEDs off:
for(int thisPin =2; thisPin <7; thisPin++){
digitalWrite(thisPin,LOW);
}
}
}
}
See Also
- serial.begin()
- serial.read()
- serial.available()
- digitalWrite()
- Arrays - A variation on the For Loop example that demonstrates how to use an array.
- ForLoopIteration - Control multiple LEDs with a for loop.
- IfStatementConditional - Use an ‘if statement’ to change the output conditions based on changing the input conditions.
- switchCase - How to choose between a discrete number of values.
- WhileStatementConditional - How to use a while loop to calibrate a sensor while a button is being read.
Last revision 2015/08/11 by SM