KS0519 Keyestudio Programming DIY Smart Sports Watch for Arduino: Difference between revisions

From Keyestudio Wiki
Jump to navigation Jump to search
No edit summary
Line 23: Line 23:
<br>[[File:0519-5.png|500px|frameless|thumb]]<br>
<br>[[File:0519-5.png|500px|frameless|thumb]]<br>
<br>[[File:0519-6.png|500px|frameless|thumb]]<br>
<br>[[File:0519-6.png|500px|frameless|thumb]]<br>
Prepare the parts as follows:


== '''Function''' ==
== '''Function''' ==

Revision as of 13:52, 19 October 2020


thumb


Introduction


thumb

In general sense, the watch is ubiquitous in our life. So how about making a watch on your own?
The new rolled out smart sports watch by Keyes team can be taken as a step counter, an ordinary watch or an alarm clock.
That sounds cool, right?
You could figure out its structure and knowledge without worrying about the annoying operation process.
The different tutorials are also provided for you. Alongside that, combing them with basic Arduino knowledge would contribute to customize your own watch readily.


Product Kit

Check the components in this kit first when you get this watch.
thumb

Installation


thumb

thumb

thumb

Function

  • (1) Built-in clock chip-----display time on screen through APP.
  • (2)Inclusive of buzzer and motor----when setting the alarm clock on App, the watch will buzz and sound.
  • (3)The screen shows the number of steps
  • (4)Short press button to switch dialplate.
  • (5)Long press button to make screen all black, and hold down again, the screen will be bright.
  • (6)The screen shows the battery capacity
  • (7)Adjust the screen brightness by APP
  • (8)The lithium battery is included, which is rechargeable


Installation

Prepare the parts as follows:
thumb

thumb

thumb


Application


thumb

  • (1)The watch boots and displays time and steps when pulling up the power switch, The steps will increase when walking.
  • (2)Short press function button, the set alarm clock and battery voltage will be shown. Press again to come back to initial interface; long press it, the screen will sleep or bright.
  • (3) Pull up the Bluetooth switch, the blue LED will flash. Flashing means no Bluetooth connection; always on implies Bluetooth connected.
  • (4) Press reset button to reboot the watch.
  • (5)USB Interface is for charging and downloading program
  • (6) The watch screen can show the English letters, numbers and symbols with different color and size.
  • (7)display time: hour/minute/second
  • (8) display“week”
  • (9) display steps
  • (10) TX is indicator to send data. It blinks when burning the program.
  • (11)Bluetooth indicator-----Flashing means no Bluetooth connection; always on implies Bluetooth connected.
  • (12)RX is indicator to receive data. It flashes when burning the program.
  • (13) show the date: year/month/day

Install APP

1.iOS system

Go to App store to search keyes DIY watch and download.
thumb

2.Android system


thumb


Enter Google play to search keyes DIY watch and download.
thumb

thumb

thumb

== Install Arduino IDE and Driver ==

(1)Installing Arduino IDE

When we get control board, we need to download Arduino IDE and driver firstly.
You could download Arduino IDE from the official website:
https://www.arduino.cc/, click the SOFTWARE on the browse bar, click“DOWNLOADS” to enter download page, as shown below:


thumb


There are various versions Of IDE for Arduino, just download a version that compatible with your system, here we will show you how to download and install the windows version Arduino IDE.



thumb


There are two versions of IDE for WINDOWS system, you can choose between the Installer (.exe) and the Zip packages. We suggest you use the first one that installs directly everything you need to use the Arduino Software (IDE), including the drivers. With the Zip package you need to install the drivers manually. The Zip file is also useful if you want to create a portable installation.


thumb


You just need to click JUST DOWNLOAD.


(2)ATmega32u4 Control Chip

Firmware:Arduino Leonardo Supply voltage:3.3V


ATmega32u4 peripheral circuit:


thumb


Arduino Leonardo (ATmega32u4)pins:


thumb


ATmega32u4 has 20 digital input/output pins (of which 7 can be used as PWM outputs), 12 analog inputs, a 16 MHz crystal oscillator.
It boasts 32KB flash(of which 4KB is used by program), 2.5KB SRAM and 1KB EEPROM.
It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started. Note that ICSP (In-Circuit Serial Programming) header can not only program the firmware to Atmega32u4, but also be used as SPI communication interface.
The keyestudio Leonardo can be powered via the micro USB connection, or via an external power supply jack (DC 7-12V) or even with female headers Vin /GND (DC 7-12V).
The Leonardo differs from other Arduino boards using separate USB-Serial chip in that the ATmega32u4 has built-in USB communication, eliminating the need for a secondary processor. This allows the Leonardo to appear to a connected computer as a mouse and keyboard.


pinMode(), digitalWrite(), and digitalRead() respectively stand for pin mode, input the digital pin and read the digital pin.
Every I/O pin can be used as input or output, boasting a 20-50K Ω pull-up resistor. It can output or input the maximum 40ma current.
The special function of other pins are listed as below:
UART: 0(RX)and 1(TX)use ATmega32U4 serial to receive and send the TTL serial data. Attention that Leonardo's Serial class refers to USB (CDC) communication, and the TTL serial ports of pins 0 and 1 use the Serial1 class. (communicate with Bluetooth in the watch).

TWI:2(SDA)and 3(SCL)support communication by Wire, however, it is used to communicate with LIS3DH acceleration chip.

External Interrupt;0.1.2.3.7 expand Interrupt pins through library file:8.9.10.11.SCK,MOSI,MISO,SS


thumb

Library file address:https://github.com/GreyGnome/EnableInterrupt
(the buttons of watch use the pin D7 and Interrupt4)
Example Interrupt program:attachInterrupt(4, use, FALLING);
PWM:Pin 3, 5, 6, 9, 10, 11 and 13 support 8-bit PWM output by analogWrite()
Pin 11of watch is used to control the screen brightness by PWM output
SPI: ICSP pin, support SPI communication by SPI library.

Analog input:A0〜A5,A6~A11(digital pin 4,6,8,9,10,12) ,32U4 has 12 analog input,from A0 to A11,they can be as I / O ports. A6-A11are respectively pin 4,6,8,9,10 and12. Each analog input has 10 bit resolution(namely 1024 different values). On the default condition,analog input is 0-5V,the voltage can be changed by AREF pins.
Other pins:
AREF: read the analog signal by function analogReference().
Reset: initialize Arduino
Program via Arduino software, select Tool>board>Arduino Leonardo.

(3)Installing Driver

(4)Arduino IDE Setting

(5) Start First Program

Projects

Pins Description


thumb


How to add a library file

Step 1:After downloading well the Arduino IDE, you can right-click the icon of Arduino IDE. Find the option "Open file location" shown as below:


thumb

Step 2: Enter it to find out libraries folder, this folder is the library file of Arduino.


thumb

Step 3:Next to find out the“libraries”folder of sports watch(seen in the link: https://fs.keyestudio.com/KS0519), you just need to replicate and paste all of libraries filesthumb into the libraries of Arduino IDE.


thumb

thumb

thumb

Then the libraries of sports watch are installed successfully.

Project 1: Vibration Motor

(1) Description

thumb

In previous lesson, we install the driver of smart sports watch and burn the test code. Next, we will get started with our basic lesson--- driving vibration motor of sports watch.
Vibration motor adopts the 2N7002 MOS transistor amplifier driver which can be directly controlled by the digital port. In this way, the electrical signals can be converted in mechanic vibration, which is applied to production of wearable device, buzz notification and vibration interactive products.

In this lesson, we compile a test code to drive the vibration motor. The different vibration effects could be performed by changing the delay time in the code. When in control, plug in GND and VCC, as the S end is high level, motor vibrates; when it is low level, motor doesn’t work.


(2)Specification

  • Rated voltage: 5.0VDC
  • Working voltage: 3.0-5.3VDC
  • Rated speed: minimum 9000 rpm
  • Rated current: 60 mA max
  • Starting current: 90 mA max
  • Starting voltage: DC3.7V


(3)Circuit Diagram

thumb

(4)Test Code:

Through circuit diagram, the signal pin of vibration pin is connected to A3 pin(equal to D17)


/*
 keyestudio smart sports watch
 lesson 1
 Vibration motor
 http://www.keyestudio.com
*/
void setup()
 { 
    pinMode(A3, OUTPUT);// initialize digital pin A3 as an output.
}
void loop() // the loop function runs over and over again forever
{
   digitalWrite(A3, HIGH); // turn the  Vibration motor on 
   delay(1000); // wait for a second
   digitalWrite(A3, LOW); // turn the  Vibration motor off 
   delay(1000); // wait for a second
}//******************************************************

(5)Test Result:

thumb

Upload the test code, the vibration motor will vibrates one second and stop for once second.


(6)Code Explanation:

pinMode(A3,OUTPUT) - This function can denote that the pin is INPUT or OUTPUT.

digitalWrite(A3,HIGH) - When pin is OUTPUT, we can set it to HIGH(output 3.3V) or LOW(output 0V)


Project 2: Passive Buzzer

(1)Description

We can use Arduino to make many interactive works of which the most commonly used is acoustic-optic display.
The buzzer we introduced is a passive buzzer. It cannot be actuated by itself, but by external pulse frequencies.
Different frequencies produce different sounds. You can use Arduino to code the melody of a song, which is quite fun and simple.
It features small size, light, cost-effective and stubborn, therefore it is widely applied to a variety of equipment, electrical production and micro controller, etc.


(2)Specification:

  • Rated voltage (Vo-p): 1.5V, 3V, 12V
  • Working voltage (Vo-p): 1-2V, 3-8V, 6-15V
  • Coil resistance (Ω): 16±4, 47±5, 140±14
  • Rated current (MAX): 30mA, 40mA, 40mA
  • Minimum sound pressure: 80dB@10cm, 85dB@10cm, 85dB@10cm
  • Frequency: 2400Hz
  • Working temperature: -30-+70
  • Storage temperature: -40-+85


(3)Circuit Diagram

thumb

(4)Test Code

In this lesson, we drive buzzer to emit“tick,tick”by test code, the signal pin is linked with A4(equal to D18)

/*
keyestudio smart sports watch
Project 2.1
Buzzer
http://www.keyestudio.com
*/
int tonepin = A4; // Set the Pin of the buzzer to the digital A4
     void setup ()
     {
       pinMode (tonepin, OUTPUT); // Set the digital IO pin mode to output
     }
     void loop ()
     {
       unsigned char i, j;
       while (1)
       {
         for (i = 0; i <80; i ++) // output a frequency sound
         {
           digitalWrite (tonepin, HIGH); // Sound
           delay (1); // Delay 1ms
           digitalWrite (tonepin, LOW); // No sound
   delay (1); // Delay 1ms
    }
   for (i = 0; i <100; i ++) // output sound with another frequency
    {
   digitalWrite (tonepin,HIGH);// Sound
   delay (2); //delay 2ms
   digitalWrite (tonepin,LOW);// No sound
   delay (2);// delay 2ms
   }}}//**********************************************

(5)Test Result:

thumb

The passive buzzer will emit “tick,tick” with different tones after compiling and uploading code.


Code analysis

When we need to repeat some statements, we have to use“for”statement.
The format of for statement is shown below:

thumb

for (i = 0; i <80; i ++) - the value of i increase from 1 to 80
(5) Extension Practice:Compose a song
We will play fabulous music if we control frequency and beats well, so let’s figure out the frequency of tones. As shown below: Bass:


thumb



thumb



thumb


After knowing the frequency of tone, next to control the time the note plays. The music will be produces when every note plays a certain amount of time. The note rhythm is divided into one beat, half beat, 1/4 beat, 1/8 beat, we stipulate the time for a note to be 1, half beat is 0.5, 1/4 beat is 0.25, 1/8 beat is 0.125....., Therefore, the music is played.
We will take example of “Ode to joy”


thumb


From notation, the music is 4/4 beat.
There are special notes we need to explain:

  • 1.Normal note, like the first note 3, correspond to 350(frequency), occupy 1 beat
  • 2.The note with underline means 0.5 beat
  • 3.The note with dot()means that 0.5 beat is added, that is 1+0.5 beat
  • 4.The note with”—” represents that 1 beat is added, that is 1+1 beat.
  • 5.The two successive notes with arc imply legato, you could slightly modify the frequency of the note behind legato(need to debug it yourself), such like reducing or increasing some values, the sound will be more smoother.


Test Code

/*
keyestudio smart sports watch
Project 2.2
Buzzer music
http://www.keyestudio.com
*/
#define NTD0 -1
#define NTD1 294
#define NTD2 330
#define NTD3 350
#define NTD4 393
#define NTD5 441
#define NTD6 495
#define NTD7 556
#define NTDL1 147
#define NTDL2 165
#define NTDL3 175
#define NTDL4 196
#define NTDL5 221
#define NTDL6 248
#define NTDL7 278
#define NTDH1 589
#define NTDH2 661
#define NTDH3 700
#define NTDH4 786
#define NTDH5 882
#define NTDH6 990
#define NTDH7 112
// List all D-tuned frequencies
#define WHOLE 1
#define HALF 0.5
#define QUARTER 0.25
#define EIGHTH 0.25
#define SIXTEENTH 0.625
// List all beats
int tune [] = // List each frequency according to the notation
{
  NTD3, NTD3, NTD4, NTD5,
  NTD5, NTD4, NTD3, NTD2,
  NTD1, NTD1, NTD2, NTD3,
  NTD3, NTD2, NTD2,
  NTD3, NTD3, NTD4, NTD5,
  NTD5, NTD4, NTD3, NTD2,
  NTD1, NTD1, NTD2, NTD3,
  NTD2, NTD1, NTD1,
  NTD2, NTD2, NTD3, NTD1,
  NTD2, NTD3, NTD4, NTD3, NTD1,
  NTD2, NTD3, NTD4, NTD3, NTD2,
  NTD1, NTD2, NTDL5, NTD0,
  NTD3, NTD3, NTD4, NTD5,
  NTD5, NTD4, NTD3, NTD4, NTD2,
  NTD1, NTD1, NTD2, NTD3,
  NTD2, NTD1, NTD1
};
float durt [] = // List the beats according to the notation
{
  1,1,1,1,
  1,1,1,1,
  1,1,1,1,
  1 + 0.5,0.5,1 + 1,
  1,1,1,1,
  1,1,1,1,
  1,1,1,1,
  1 + 0.5,0.5,1 + 1,
  1,1,1,1,
  1,0.5,0.5,1,1,
  1,0.5,0.5,1,1,
  1,1,1,1,
  1,1,1,1,
  1,1,1,0.5,0.5,
  1,1,1,1,
  1 + 0.5,0.5,1 + 1,
};
int length;
int tonepin = A4; // Use interface A4
void setup ()
{
  pinMode (tonepin, OUTPUT);
  length = sizeof (tune) / sizeof (tune [0]); // Calculate length
}
void loop ()
{
  for (int x = 0; x <length; x ++)
  {
    tone (tonepin, tune [x]);
    delay (500 * durt [x]); // This is used to adjust the delay according to the beat, 500 can be adjusted by yourself.
    noTone (tonepin);
  }
  delay (2000); // delay 2s
}
//*******************************************************

Advanced version of Arduino IDE sometimes toughly identify the format of test code, consequently, the problem occurs as follows:


thumb


Please directly open the test code of libraries folder : https://fs.keyestudio.com/KS0519


thumb


Upload the code to the development board, do you hear “Ode to Joy”?

Project 3: Detect and Read Voltage(0-1024)

(1)Description


This project is used to detect voltage based on the principle of resistance divide voltage, which aims to measure the voltage over 5v and decline two times of input voltage.
The analog voltage is up to 5V, consequently, the input voltage is less than or equal to 5V×2=10V(the working voltage of control chip is 3.3v, so the battery voltage is less than or equal to 3.3Vx2=6.6V, otherwise the measurement is not correct) Arduino uses AVR chip with 10 bit AD, therefore, the resolution of this module is 0.0032V(3.3V/1023).


(2) Circuit Diagram


thumb



(3) Test Code

Through circuit diagram, we know the signal pin detected by voltage is connected to A5 pin.


/*
keyestudio smart sports watch
Project 3
Voltage detection
http://www.keyestudio.com
*/
const int Electric = A5;
  void setup()
   {
  Serial.begin(9600);
  pinMode(Electric,INPUT);
   }

void loop() {
  float dian=analogRead(Electric);
  float DL=(dian/1024*3.3)*2;//convert battery capacity
  Serial.print(DL);
  Serial.println("V");
  delay(1000);
}//******************************************************

(4) Test Result


thumb


Let’s compile code, open serial monitor and set baud rate to 9600. We can view the battery capacity.

Project 4: Button Switch