GY-521 module is a breakout board for the MPU-6050 - with I2C interface features a 6-Axis sensors (gyroscope and accelerometer) and temperature sensor.
The MPU-60X0 is the world’s first integrated 6-axis MotionTracking device that combines a 3-axis gyroscope, 3-axis accelerometer, and a Digital Motion Processor™ (DMP) all in a small 4x4x0.9mm package. With its dedicated I2C sensor bus.... ...Additional features include an embedded temperature sensor...
Notice on the Product Page, stated that it is in production, but Not Recommended for New Design. Recommended Alternate is ICM-20602 (Interchangeability is not guaranteed.)
Anyway, I have a sample unit of GY-521 on hand. It's a exercise of using Arduino Uno to read GY-521 via I2C, using MPU6050_tockn, using Arduino library for easy communication with MPU6050.
Connect Arduino Uno with GY-521:
- Start Arduino IDE, make sure you select the correct board and port.
- Menu : Sketch > Include Library > Manage Library... to open Library Manager.
- Search MPU6050 to install MPU6050_tockn library.
- Menu : File > Examples > MPU6050_tockn > GetAllData
- Upload to Uno, and open Serial Monitor. To check the output.
Now we modify a little bit, such that we can see the result more easy visually.
As you try Arduino example on PlatformIO. PlatformIO may throw many "not declared in this scope" errors. Because PlatformIO require Forward Declaration of function. To fix it, copy to define the function prototype before the functions called, as show in the video.
Edit platformio.ini to add "lib_deps = Wire" to fix the error of "Wire.h: No such file or directory". And specify upload_port if the IDE cannot detect your port.
Build and upload the program to Uno, then open Platform IO Serial Monitor. Once calibration finished, the calibration data will be show in Serial Monitor, copy it.
Here is the result of my board:
*** COPY-PASTE from Serial Terminal:
const int XP=8,XM=A2,YP=A3,YM=9; //240x320 ID=0x9341
const int TS_LEFT=169,TS_RT=941,TS_TOP=176,TS_BOT=927;
Simple Touch to draw example:
Create another Project in Platform IO IDE for simple Touch to draw example, target Arduino Uno.
Replace the calibrate data (copy from the above program) in the code:
Edit platformio.ini to add "lib_deps = Wire" to fix the error of "Wire.h: No such file or directory". And specify upload_port if the IDE cannot detect your port.
It's a 2.8" 320*240 TFT Touch Screen shield, with driver ILI9341 connected using 8 bit parallel interface. Support Arduino UNO and Mega2560 direct plug-in use without wiring.
You can find the details of the TFT Touch Screen Shield from the above link. Also provide Program Download with examples. But I prefer use libraries come with Platform IO IDE. This video show how to:
To burn bootloader to Arduino MEGA 2560 using Arduino Uno as ISP, connect Uno & MEGA as shown:
PC is connected to Uno.
- Program Uno as ArduinoISP
> Select board of Uno
> Select programmer of AVRISP mkII
> File > Examples > ArduinoISP > ArduinoISP
> Upload
- Now we are going to program MEGA, the Uno act as Arduino as ISP.
> Select board of MEGA
> Select programmer of Arduino as ISP
> Tools > Burn Bootloader
We can add LedControl Library to Arduino IDE, to control 8*8 LED Matrix with MAX7219 via SPI. LedControl is a library for the MAX7219 and the MAX7221 Led display driver.
(ref: http://wayoda.github.io/LedControl/)
Open the File > Examples > LedControl > LCDemoMatrix
LCDemoMatrix.ino
//We always have to include the library
#include "LedControl.h"
/*
Now we need a LedControl to work with.
***** These pin numbers will probably not work with your hardware *****
pin 12 is connected to the DataIn
pin 11 is connected to the CLK
pin 10 is connected to LOAD
We have only a single MAX72XX.
*/
LedControl lc=LedControl(12,11,10,1);
/* we always wait a bit between updates of the display */
unsigned long delaytime=100;
void setup() {
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
lc.shutdown(0,false);
/* Set the brightness to a medium values */
lc.setIntensity(0,8);
/* and clear the display */
lc.clearDisplay(0);
}
/*
This method will display the characters for the
word "Arduino" one after the other on the matrix.
(you need at least 5x7 leds to see the whole chars)
*/
void writeArduinoOnMatrix() {
/* here is the data for the characters */
byte a[5]={B01111110,B10001000,B10001000,B10001000,B01111110};
byte r[5]={B00111110,B00010000,B00100000,B00100000,B00010000};
byte d[5]={B00011100,B00100010,B00100010,B00010010,B11111110};
byte u[5]={B00111100,B00000010,B00000010,B00000100,B00111110};
byte i[5]={B00000000,B00100010,B10111110,B00000010,B00000000};
byte n[5]={B00111110,B00010000,B00100000,B00100000,B00011110};
byte o[5]={B00011100,B00100010,B00100010,B00100010,B00011100};
/* now display them one by one with a small delay */
lc.setRow(0,0,a[0]);
lc.setRow(0,1,a[1]);
lc.setRow(0,2,a[2]);
lc.setRow(0,3,a[3]);
lc.setRow(0,4,a[4]);
delay(delaytime);
lc.setRow(0,0,r[0]);
lc.setRow(0,1,r[1]);
lc.setRow(0,2,r[2]);
lc.setRow(0,3,r[3]);
lc.setRow(0,4,r[4]);
delay(delaytime);
lc.setRow(0,0,d[0]);
lc.setRow(0,1,d[1]);
lc.setRow(0,2,d[2]);
lc.setRow(0,3,d[3]);
lc.setRow(0,4,d[4]);
delay(delaytime);
lc.setRow(0,0,u[0]);
lc.setRow(0,1,u[1]);
lc.setRow(0,2,u[2]);
lc.setRow(0,3,u[3]);
lc.setRow(0,4,u[4]);
delay(delaytime);
lc.setRow(0,0,i[0]);
lc.setRow(0,1,i[1]);
lc.setRow(0,2,i[2]);
lc.setRow(0,3,i[3]);
lc.setRow(0,4,i[4]);
delay(delaytime);
lc.setRow(0,0,n[0]);
lc.setRow(0,1,n[1]);
lc.setRow(0,2,n[2]);
lc.setRow(0,3,n[3]);
lc.setRow(0,4,n[4]);
delay(delaytime);
lc.setRow(0,0,o[0]);
lc.setRow(0,1,o[1]);
lc.setRow(0,2,o[2]);
lc.setRow(0,3,o[3]);
lc.setRow(0,4,o[4]);
delay(delaytime);
lc.setRow(0,0,0);
lc.setRow(0,1,0);
lc.setRow(0,2,0);
lc.setRow(0,3,0);
lc.setRow(0,4,0);
delay(delaytime);
}
/*
This function lights up a some Leds in a row.
The pattern will be repeated on every row.
The pattern will blink along with the row-number.
row number 4 (index==3) will blink 4 times etc.
*/
void rows() {
for(int row=0;row<8;row++) {
delay(delaytime);
lc.setRow(0,row,B10100000);
delay(delaytime);
lc.setRow(0,row,(byte)0);
for(int i=0;i<row;i++) {
delay(delaytime);
lc.setRow(0,row,B10100000);
delay(delaytime);
lc.setRow(0,row,(byte)0);
}
}
}
/*
This function lights up a some Leds in a column.
The pattern will be repeated on every column.
The pattern will blink along with the column-number.
column number 4 (index==3) will blink 4 times etc.
*/
void columns() {
for(int col=0;col<8;col++) {
delay(delaytime);
lc.setColumn(0,col,B10100000);
delay(delaytime);
lc.setColumn(0,col,(byte)0);
for(int i=0;i<col;i++) {
delay(delaytime);
lc.setColumn(0,col,B10100000);
delay(delaytime);
lc.setColumn(0,col,(byte)0);
}
}
}
/*
This function will light up every Led on the matrix.
The led will blink along with the row-number.
row number 4 (index==3) will blink 4 times etc.
*/
void single() {
for(int row=0;row<8;row++) {
for(int col=0;col<8;col++) {
delay(delaytime);
lc.setLed(0,row,col,true);
delay(delaytime);
for(int i=0;i<col;i++) {
lc.setLed(0,row,col,false);
delay(delaytime);
lc.setLed(0,row,col,true);
delay(delaytime);
}
}
}
}
void loop() {
writeArduinoOnMatrix();
rows();
columns();
single();
}
Connect MAX7219 8x8 LED Matrix to Arduino Uno as stated in the example:
- pin 12 is connected to the DataIn
- pin 11 is connected to the CLK
- pin 10 is connected to LOAD (it's cs marked on my sample)
- +5V to VCC
- GND to GND
For my on-hand HC-05 sample, it set as slave role and 9600, 0, 0, PIN="1234" by default. So it can be used in this example without any extra setting.
Connection between UNO and HC-05
HC-05 Rx - Uno Tx (1)
HC-05 Tx - Uno Rx (0)
HC-05 GND - Uno GND
HC-05 VCC - Uno 5V (My HC-05 marked "Power: 3.6V-6V", refer here, make sure your HC-05 can work on 5V.)
Uno_Serial_echo.ino
/*
Arduino Uno + HC-05 (Bluetooth) - echo bluetooth data
Serial (Tx/Rx) communicate to HC-05
HC-05 Rx - Uno Tx (1)
HC-05 Tx - Uno Rx (0)
HC-05 GND - Uno GND
HC-05 VCC - Uno 5V
*/
void setup()
{
delay(1000);
Serial.begin(9600);
}
void loop()
{
while(Serial.available())
{
char data = Serial.read();
Serial.write(data);
}
}
This video show how Android BluetoothChat example link with Arduino UNO + HC-05. For the Android BluetoothChat example, refer last post.
You can download ZIP file from the library web page, and it to Arduino library, read the video below. After library added, it will be in the folder your_Documents\Arduino\libraries\,
and fritzing parts is in your_Documents\Arduino\libraries\rfid-master\doc\fritzing\.
Connect Arduino Uno and RFID-RF522 module:
In Arduino IDE, Open Example of DumpInfo in MFRC522:
/*
* ----------------------------------------------------------------------------
* This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid
* for further details and other examples.
*
* NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
*
* Released into the public domain.
* ----------------------------------------------------------------------------
* Example sketch/program showing how to read data from a PICC (that is: a RFID
* Tag or Card) using a MFRC522 based RFID Reader on the Arduino SPI interface.
*
* When the Arduino and the MFRC522 module are connected (see the pin layout
* below), load this sketch into Arduino IDE then verify/compile and upload it.
* To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M).
* When you present a PICC (that is: a RFID Tag or Card) at reading distance
* of the MFRC522 Reader/PCD, the serial output will show the ID/UID, type and
* any data blocks it can read. Note: you may see "Timeout in communication"
* messages when removing the PICC from reading distance too early.
*
* If your reader supports it, this sketch/program will read all the PICCs
* presented (that is: multiple tag reading). So if you stack two or more
* PICCs on top of each other and present them to the reader, it will first
* output all details of the first and then the next PICC. Note that this
* may take some time as all data blocks are dumped, so keep the PICCs at
* reading distance until complete.
*
* Typical pin layout used:
* -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro
* Signal Pin Pin Pin Pin Pin Pin
* -----------------------------------------------------------------------------------------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
* SPI SS SDA(SS) 10 53 D10 10 10
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*/
#include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9 //
#define SS_PIN 10 //
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
Serial.println(F("Scan PICC to see UID, type, and data blocks..."));
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}
// Dump debug info about the card; PICC_HaltA() is automatically called
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}
void ShowReaderDetails() {
// Get the MFRC522 software version
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print(F("MFRC522 Software Version: 0x"));
Serial.print(v, HEX);
if (v == 0x91)
Serial.print(F(" = v1.0"));
else if (v == 0x92)
Serial.print(F(" = v2.0"));
else
Serial.print(F(" (unknown)"));
Serial.println("");
// When 0x00 or 0xFF is returned, communication probably failed
if ((v == 0x00) || (v == 0xFF)) {
Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
}
}
Host development platform:
OS: Windows 10
IDE: NetBeans IDE 8.0.2
Programming Language: Java + JavaFX + jSSC
Target platform:
Raspberry Pi 2
OS: Raspbian
IP: 192.168.1.112
Both Host development platform and Target platform in the same Network.
(How to set Remote Java SE Platform to deploy on Raspberry Pi, refer to the video in the post "Java + JavaFX + jSSC run on Raspberry Pi, control Arduino Uno")
remark: due to something wrong on my Raspberry Pi 2 cannot detect monitor correctly, I have to edit /boot/config.txt to set framebuffer_width and framebuffer_height to 500x400. So the screen output may be differency to you.
Arduino Side:
Board: Arduino Uno + 8x8 LED Matrix
Connected to Raspberry Pi 2 with USB. Arduino Side:
Arduino code and connection between Arduino Uno and 8x8 LED Matrix, refer last post.
Java/JavaFX/jSSC code, program on Windows 10/NetBeans, run on Raspberry Pi 2:
(Basically same as last post, with moving btnExit to vBoxMatrix, such that user can exit the program in raspberry Pi UI.
This video show how to create Remote Java SE Platform on NetBeans IDE run on Windows 10, and run it remotely on Raspberry Pi 2.
Host development platform:
OS: Windows 10
IDE: NetBeans IDE 8.0.2
Programming Language: Java + JavaFX + jSSC (refer last post Last Post)
Target platform:
Raspberry Pi 2
OS: Raspbian
IP: 192.168.1.110
Both Host development platform and Target platform in the same Network. remark: due to something wrong on my Raspberry Pi 2 cannot detect monitor correctly, I have to edit /boot/config.txt to set framebuffer_width and framebuffer_height to 500x400. So the screen output may be differency to you.
Arduino Side:
Board: Arduino Uno
Connected to Raspberry Pi 2 with USB.
Program and Connection: (refer last post Last Post)
This example show Bi-direction communication between Arduino Uno and PC using Java + javaFX + jSSC:
Arduino to PC: Arduino Uno analog input, display on JavaFX LineChart
PC to Arduino: Button to control Arduino Uno on-board LED
This example show how to use Java + jSSC + JavaFX running on PC/Windows 10, read bytes from USB/Serial. The data sent from Arduino Uno by reading analog input. The PC and Arduino Uno connected with USB.
/*
* Example of using jSSC library to handle serial port
* Receive number from Arduino via USB/Serial and display on Label
*/
package javafx_jssc_uno;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import jssc.SerialPort;
import static jssc.SerialPort.MASK_RXCHAR;
import jssc.SerialPortEvent;
import jssc.SerialPortException;
import jssc.SerialPortList;
public class JavaFX_jssc_Uno extends Application {
SerialPort arduinoPort = null;
ObservableList<String> portList;
Label labelValue;
private void detectPort(){
portList = FXCollections.observableArrayList();
String[] serialPortNames = SerialPortList.getPortNames();
for(String name: serialPortNames){
System.out.println(name);
portList.add(name);
}
}
@Override
public void start(Stage primaryStage) {
labelValue = new Label();
labelValue.setFont(new Font("Arial", 150));
detectPort();
final ComboBox comboBoxPorts = new ComboBox(portList);
comboBoxPorts.valueProperty()
.addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable,
String oldValue, String newValue) {
System.out.println(newValue);
disconnectArduino();
connectArduino(newValue);
}
});
VBox vBox = new VBox();
vBox.getChildren().addAll(
comboBoxPorts, labelValue);
StackPane root = new StackPane();
root.getChildren().add(vBox);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public boolean connectArduino(String port){
System.out.println("connectArduino");
boolean success = false;
SerialPort serialPort = new SerialPort(port);
try {
serialPort.openPort();
serialPort.setParams(
SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setEventsMask(MASK_RXCHAR);
serialPort.addEventListener((SerialPortEvent serialPortEvent) -> {
if(serialPortEvent.isRXCHAR()){
try {
byte[] b = serialPort.readBytes();
int value = b[0] & 0xff; //convert to int
String st = String.valueOf(value);
//Update label in ui thread
Platform.runLater(() -> {
labelValue.setText(st);
});
} catch (SerialPortException ex) {
Logger.getLogger(JavaFX_jssc_Uno.class.getName())
.log(Level.SEVERE, null, ex);
}
}
});
arduinoPort = serialPort;
success = true;
} catch (SerialPortException ex) {
Logger.getLogger(JavaFX_jssc_Uno.class.getName())
.log(Level.SEVERE, null, ex);
System.out.println("SerialPortException: " + ex.toString());
}
return success;
}
public void disconnectArduino(){
System.out.println("disconnectArduino()");
if(arduinoPort != null){
try {
arduinoPort.removeEventListener();
if(arduinoPort.isOpened()){
arduinoPort.closePort();
}
} catch (SerialPortException ex) {
Logger.getLogger(JavaFX_jssc_Uno.class.getName())
.log(Level.SEVERE, null, ex);
}
}
}
@Override
public void stop() throws Exception {
disconnectArduino();
super.stop();
}
public static void main(String[] args) {
launch(args);
}
}
Arduino Uno side - AnalogInputToUSB.ino
/*
* AnalogInputUSB
* Read analog input from analog pin 0
* and send data to USB
*/
int ledPin = 13;
int analogPin = A0;
int analogValue = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(ledPin, HIGH);
analogValue = analogRead(analogPin); //Read analog input
analogValue = map(analogValue, 0, 1023, 0, 255);
Serial.write(analogValue); //write as byte, to USB
digitalWrite(ledPin, LOW);
delay(1000);
}
Connect a potentiometer in Analog Input A0 as input.
Prepare a simple sketch run on Arduino Uno to send a counting number to serial port, tested on Windows 10.
BlinkUSB.ino
/*
* Send number to Serial
*/
int i = 0;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
Serial.begin(9600);
}
// the loop function runs over and over again forever
void loop() {
Serial.print(i);
i++;
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
It's a 1.3"" 128X64 OLED module, I2C/SPI interface (4-wire SPI selected), with SH1106 driver (SSD1306 compatible), 3.3/5V compatible. This post show how to modify from HelloWorld of u8glib library, to make it work. TO download and install u8glib library, refer to last post "Hello World 0.96 inch 128X64 I2C OLED, on Arduino Uno, using u8glib library". It work on both Arduino Uno with 5V, and Arduino Due with 3.3V.
Connection between the OLED module to Arduino Uno (Arduino Due): 4 wire SPI OLED - Arduino Uno (Due)
GND - GND
VCC - 5V (3.3V for Arduino Due)
D0 - 10
D1 - 9
RST - 13
DC - 11
CS - 12
To make the HelloWorld work with this OLED module, add the following constructor calls: U8GLIB_SH1106_128X64 u8g(10, 9, 12, 11, 13);