Week 14 - Networking and communications¶
Group Assignment¶
- send a message between two projects
DigitalRead & DigitalWrite¶
Using boards:
- Supply power: Arduino Uno
- Transmit(tx) board: Attiny44 Hello world board from week6
- Receive(rx) board: Attiny45 core module board from week14
- receive power: Charlieplexing board from week10
refer to Serial communication between two ATtiny boards We want to control LEDs on charlieplexing board in the beginning, but this board doesn’t have rx/tx pins. So another idea is using another attiny45 board to control its power. And using button on attiny44 hello world board as a trigger.
The code is simply using digitalWrite on TX board (attiny44 hello world) and digitalRead on RX board (attiny45).
Code for tx board¶
const int button = 2; // pin PA2 on ATtiny44a
const int led = 3; // pin PA3 on ATtiny44a
const int tx = 1; // pin PA1 on ATtiny44a
int buttonData;
void setup() {
pinMode(led, OUTPUT);
pinMode(button, INPUT);
pinMode(tx, OUTPUT);
}
void loop() { // run over and over
buttonData = digitalRead(button);
if(buttonData == LOW){
digitalWrite(tx, HIGH);
digitalWrite(led, HIGH);
}
else if(buttonData == HIGH){
digitalWrite(tx, LOW);
digitalWrite(led, LOW);
}
}
Code for rx board¶
int readData;
void setup() {
pinMode(PB3, OUTPUT);
pinMode(PB2, INPUT);
}
void loop() {
readData = digitalRead(PB2);
if(readData == HIGH){
digitalWrite(PB3,HIGH);
}
else{
digitalWrite(PB3,LOW);
}
}
Serial Communication between boards¶
Next try with the SoftwareSerial
library. Again using boards:
- Supply power: Arduino Uno
- Transmit(tx) board: Attiny45 core module board from week14
- Receive(rx) board: Attiny44 Hello world board from week6
- Debug: Serial to FTDI converter
This time my goal is programming attiny 45 board to control attiny44 board flashing LED. And both boards have FTDI pins so can use them to debug.
code for TX board¶
#include <SoftwareSerial.h>
SoftwareSerial mySerial(PB3,PB4); // RX, TX
int readData;
void setup() {
mySerial.begin(9600);
}
void loop() { // run over and over
mySerial.print("1");
delay(500);
mySerial.print("2");
delay(500);
}
code for RX board¶
#include <SoftwareSerial.h>
SoftwareSerial mySerial(PA0,PA1); //RX, TX
int v;
void setup() {
mySerial.begin(3840);
pinMode(3,OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
v = mySerial.read();
if(v == '1'){
mySerial.println(v);
digitalWrite(3,HIGH);
}
else if(v == '2'){
mySerial.println(v);
digitalWrite(3,LOW);
}
}
Somwthing to note: “baud rate” should consider the clock speed. In this case, my attiny45 board hae internal 8 mhz crystal, while my attiny44 board has external 20 mhz crystal (2.5 times). Thus 9600 baud rate / 8 mhz speed corresponding to 3840 baud rate / 20 mhz speed. Update: later test with “atmega328 with external 16mhz crystal” & “attiny45 with internal 8mhz”, set 9600 baud rate for both of them works fine. Not sure why is that.
Another thing is that when using serial.print()
we use character type (“”), but when using serial.read()
we use integer type (‘’).
Serial Communication from computer to boards¶
refer to xinhui-hu last try to send message from computer to boards. Again using boards:
- Supply power / serial adapter converter: Arduino Uno
- Transmit(tx) board: Attiny45 core module board from week14
- Receive(rx) board: Attiny44 Hello world board from week6
Code for Ardeuino Uno¶
void setup(){
pinMode(0,INPUT); // become tx, sending message from Computer to target board
pinMode(1,INPUT); // become rx, receiving message from target board to computer
}
void loop(){
}
refer to 3 Ways to Use Arduino as USB Serial Adapter Converter, this sketch make Arduino become a serial adapter converter without using wires or detach chip.
Code for TX board¶
#include <SoftwareSerial.h>
SoftwareSerial mySerial(PB3,PB4); // RX, TX
int readData;
void setup() {
mySerial.begin(9600);
// pinMode(8,OUTPUT);
}
void loop() { // run over and over
readData = mySerial.read();
if(readData == '1'){
mySerial.print("1");
}
if(readData == '2'){
mySerial.print("2");
}
}
Code for RX board¶
remain the same from “Serial Communication between boards”.