├── GY87 └── GY87.ino ├── LICENSE ├── README.md └── sercom └── sercom.ino /GY87/GY87.ino: -------------------------------------------------------------------------------- 1 | /* 2 | GY-87 Test 3 | 4 | Tests basic functionality of the GY-87 sensor board. 5 | 6 | Connections: 7 | |== GY-87 ==|== Arduino ==| 8 | | VCC_IN | VCC | 9 | | GND | GND | 10 | | SDA | SDA | 11 | | SCL | SCL | 12 | |=========================| 13 | 14 | Requires the I2CDevLib library, which can be found here: https://github.com/jrowberg/i2cdevlib 15 | 16 | by Tom Kuehn 17 | 26/06/2016 18 | */ 19 | 20 | #include "I2Cdev.h" 21 | #include "MPU6050.h" 22 | #include "HMC5883L.h" 23 | #include "BMP085.h" 24 | #include "Wire.h" 25 | 26 | static const char LED = 6; 27 | static const float ACCEL_SENS = 16384.0; // Accel Sensitivity with default +/- 2g scale 28 | static const float GYRO_SENS = 131.0; // Gyro Sensitivity with default +/- 250 deg/s scale 29 | 30 | // Magnetometer class default I2C address is 0x1E 31 | // specific I2C addresses may be passed as a parameter here 32 | // this device only supports one I2C address (0x1E) 33 | HMC5883L mag; 34 | int16_t mx, my, mz; 35 | 36 | // Accel/Gyro class default I2C address is 0x68 (can be 0x69 if AD0 is high) 37 | // specific I2C addresses may be passed as a parameter here 38 | MPU6050 accelgyro; 39 | 40 | int16_t ax, ay, az; 41 | int16_t gx, gy, gz; 42 | 43 | // Barometer class default I2C address is 0x77 44 | // specific I2C addresses may be passed as a parameter here 45 | // (though the BMP085 supports only one address) 46 | BMP085 barometer; 47 | 48 | float temperature; 49 | float pressure; 50 | int32_t lastMicros; 51 | 52 | void setup() 53 | { 54 | boolean state = HIGH; 55 | unsigned int count = 0; 56 | 57 | pinMode(LED, OUTPUT); 58 | 59 | Serial.begin(9600); 60 | while (!Serial && (count < 30) ) 61 | { 62 | delay(200); // Wait for serial port to connect with timeout. Needed for native USB 63 | digitalWrite(LED, state); 64 | state = !state; 65 | count++; 66 | } 67 | 68 | digitalWrite(LED, HIGH); 69 | 70 | // join I2C bus (I2Cdev library doesn't do this automatically) 71 | Wire.begin(); 72 | 73 | // ==================== MPU6050 ============================ 74 | accelgyro.initialize(); 75 | Serial.print("Testing Accel/Gyro... "); 76 | Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); 77 | 78 | // Starts up with accel +/- 2 g and gyro +/- 250 deg/s scale 79 | accelgyro.setI2CBypassEnabled(true); // set bypass mode 80 | // Now we can talk to the HMC5883l 81 | 82 | // ==================== HMC5883L ============================ 83 | mag.initialize(); 84 | Serial.print("Testing Mag... "); 85 | Serial.println(mag.testConnection() ? "HMC5883L connection successful" : "HMC5883L connection failed"); 86 | 87 | // ==================== BMP085 ============================ 88 | barometer.initialize(); 89 | Serial.print("Testing Pressure... "); 90 | Serial.println(barometer.testConnection() ? "BMP085 connection successful" : "BMP085 connection failed"); 91 | 92 | Serial.println("Setup Complete"); 93 | } 94 | 95 | void loop() 96 | { 97 | static unsigned long ms = 0; 98 | static boolean state = HIGH; 99 | 100 | // Serial Output Format 101 | // === Accel === | === Gyro === | ======= Mag ======= | === Barometer === | 102 | // X Y Z | X Y Z | X Y Z Heading | Temp Pressure | 103 | 104 | if (millis() - ms > 100) 105 | { 106 | // read raw accel/gyro measurements 107 | accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); 108 | 109 | // display tab-separated accel/gyro x/y/z values 110 | Serial.print(ax/ACCEL_SENS); Serial.print("\t"); 111 | Serial.print(ay/ACCEL_SENS); Serial.print("\t"); 112 | Serial.print(az/ACCEL_SENS); Serial.print("\t"); 113 | Serial.print(gx/GYRO_SENS); Serial.print("\t"); 114 | Serial.print(gy/GYRO_SENS); Serial.print("\t"); 115 | Serial.print(gz/GYRO_SENS); Serial.print("\t"); 116 | 117 | // read raw heading measurements 118 | mag.getHeading(&mx, &my, &mz); 119 | 120 | // display tab-separated mag x/y/z values 121 | Serial.print(mx); Serial.print("\t"); 122 | Serial.print(my); Serial.print("\t"); 123 | Serial.print(mz); Serial.print("\t"); 124 | 125 | // To calculate heading in degrees. 0 degree indicates North 126 | float heading = atan2(my, mx); 127 | if(heading < 0) heading += 2 * M_PI; 128 | Serial.print(heading * 180/M_PI); Serial.print("\t"); 129 | 130 | // request temperature 131 | barometer.setControl(BMP085_MODE_TEMPERATURE); 132 | 133 | // wait appropriate time for conversion (4.5ms delay) 134 | lastMicros = micros(); 135 | while (micros() - lastMicros < barometer.getMeasureDelayMicroseconds()); 136 | 137 | // read calibrated temperature value in degrees Celsius 138 | temperature = barometer.getTemperatureC(); 139 | 140 | // request pressure (3x oversampling mode, high detail, 23.5ms delay) 141 | barometer.setControl(BMP085_MODE_PRESSURE_3); 142 | while (micros() - lastMicros < barometer.getMeasureDelayMicroseconds()); 143 | 144 | // read calibrated pressure value in Pascals (Pa) 145 | pressure = barometer.getPressure(); 146 | 147 | // display measured values if appropriate 148 | Serial.print(temperature); Serial.print("\t"); 149 | Serial.print(pressure/100); Serial.println("\t"); 150 | 151 | ms = millis(); 152 | digitalWrite(LED, state); 153 | state = !state; 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Tom K 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Arduino-Test 2 | Arduino test sketches. 3 | 4 | ## Sercom 5 | *For MKR1000 only* 6 | Test sketch with 4 serial ports: 7 | - Serial - Native USB interface 8 | - Serial1 - Default serial port on D13, D14 (Sercom 5) 9 | - Serial2 - Extra serial port on D0, D1 (Sercom 3) 10 | - Serial3 - Extra serial port on D4, D5 (Sercom 4) 11 | 12 | No more can be added with out reconfiguring the SPI or Wire interfaces. 13 | 14 | ## GY-87 15 | Tests basic functionality of the GY-87 sensor board. The board is small, cheap and contains an accelerometer, gyro, magnetometer and barometric pressure sensor. Requires the [I2Cdevlib](https://github.com/jrowberg/i2cdevlib) library. 16 | 17 | -------------------------------------------------------------------------------- /sercom/sercom.ino: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | SERCOM Test 4 | 5 | Test the ability to add extra hardware serial ports to the MKR1000 6 | This sketch has the following serial interfaces: 7 | Serial - Native USB interface 8 | Serial1 - Default serial port on D13, D14 (Sercom 5) 9 | Serial2 - Extra serial port on D0, D1 (Sercom 3) 10 | Serial3 - Extra serial port on D4, D5 (Sercom 4) 11 | 12 | Good explanation of sercom funcationality here: 13 | https://learn.adafruit.com/using-atsamd21-sercom-to-add-more-spi-i2c-serial-ports/muxing-it-up 14 | 15 | This sketch will echo characters recieved on any of the 4 serial ports to all other serial ports. 16 | 17 | for Arduino MKR1000 18 | by Tom Kuehn 19 | 26/06/2016 20 | 21 | */ 22 | 23 | #include // required before wiring_private.h 24 | #include 25 | 26 | static const char MKR1000_LED = 6; 27 | 28 | // Serial2 pin and pad definitions (in Arduino files Variant.h & Variant.cpp) 29 | #define PIN_SERIAL2_RX (1ul) // Pin description number for PIO_SERCOM on D1 30 | #define PIN_SERIAL2_TX (0ul) // Pin description number for PIO_SERCOM on D0 31 | #define PAD_SERIAL2_TX (UART_TX_PAD_0) // SERCOM pad 0 TX 32 | #define PAD_SERIAL2_RX (SERCOM_RX_PAD_1) // SERCOM pad 1 RX 33 | 34 | // Serial3 pin and pad definitions (in Arduino files Variant.h & Variant.cpp) 35 | #define PIN_SERIAL3_RX (5ul) // Pin description number for PIO_SERCOM on D5 36 | #define PIN_SERIAL3_TX (4ul) // Pin description number for PIO_SERCOM on D4 37 | #define PAD_SERIAL3_TX (UART_TX_PAD_2) // SERCOM pad 2 TX 38 | #define PAD_SERIAL3_RX (SERCOM_RX_PAD_3) // SERCOM pad 3 RX 39 | 40 | // Instantiate the extra Serial classes 41 | Uart Serial2(&sercom3, PIN_SERIAL2_RX, PIN_SERIAL2_TX, PAD_SERIAL2_RX, PAD_SERIAL2_TX); 42 | Uart Serial3(&sercom4, PIN_SERIAL3_RX, PIN_SERIAL3_TX, PAD_SERIAL3_RX, PAD_SERIAL3_TX); 43 | 44 | void SERCOM3_Handler() // Interrupt handler for SERCOM3 45 | { 46 | Serial2.IrqHandler(); 47 | } 48 | 49 | void SERCOM4_Handler() // Interrupt handler for SERCOM4 50 | { 51 | Serial3.IrqHandler(); 52 | } 53 | 54 | void setup() 55 | { 56 | static boolean state = HIGH; 57 | static unsigned char count = 0; 58 | 59 | pinMode(MKR1000_LED, OUTPUT); 60 | Serial1.begin(9600); 61 | 62 | // Start Serial for debugging on the Serial Monitor 63 | Serial.begin(9600); 64 | while (!Serial && (count < 30) ) 65 | { 66 | delay(200); // Wait for serial port to connect with timeout. Needed for native USB 67 | digitalWrite(MKR1000_LED, state); 68 | state = !state; 69 | count++; 70 | } 71 | 72 | pinPeripheral(0, PIO_SERCOM); // Assign pins 0 & 1 SERCOM functionality 73 | pinPeripheral(1, PIO_SERCOM); 74 | Serial2.begin(57600); // Begin Serial2 75 | 76 | pinPeripheral(4, PIO_SERCOM_ALT); // Assign pins 4 & 5 SERCOM functionality 77 | pinPeripheral(5, PIO_SERCOM_ALT); 78 | Serial3.begin(57600); // Begin Serial3 79 | 80 | digitalWrite(MKR1000_LED, HIGH); 81 | 82 | Serial.println("Setup Complete"); 83 | Serial1.println("Setup Complete"); 84 | Serial2.println("Setup Complete"); 85 | Serial3.println("Setup Complete"); 86 | } 87 | 88 | void loop() 89 | { 90 | static unsigned long ms = 0; 91 | static boolean state = HIGH; 92 | static unsigned char c = 0; 93 | 94 | if (Serial.available()) 95 | { 96 | char c = Serial.read(); 97 | Serial.print(c); 98 | Serial1.print(c); 99 | Serial2.print(c); 100 | Serial3.print(c); 101 | } 102 | if (Serial1.available()) 103 | { 104 | char c = Serial1.read(); 105 | Serial.print(c); 106 | Serial1.print(c); 107 | Serial2.print(c); 108 | Serial3.print(c); 109 | } 110 | if (Serial2.available()) 111 | { 112 | char c = Serial2.read(); 113 | Serial.print(c); 114 | Serial1.print(c); 115 | Serial2.print(c); 116 | Serial3.print(c); 117 | } 118 | if (Serial3.available()) 119 | { 120 | char c = Serial3.read(); 121 | Serial.print(c); 122 | Serial1.print(c); 123 | Serial2.print(c); 124 | Serial3.print(c); 125 | } 126 | 127 | 128 | if (millis() - ms > 100) 129 | { 130 | ms = millis(); 131 | digitalWrite(MKR1000_LED, state); 132 | state = !state; 133 | } 134 | 135 | } 136 | --------------------------------------------------------------------------------