Hardware components | ||||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
Software apps and online services | ||||||
|
In this project we will show off PWM with a mini fan using a TI LaunchPad (www.ti.com/launchpad). Programmed using Energia (www.energia.nu)
The 4 digit display will show off the current PWM value being sent to the motor driver.
As the LaunchPad increments the PWM, the motor will spin up with greater power. Once it hits 255, the top range of the PWM, it decrements back down and the motor begins to slow down until it stops.
To make the wiring easier I am using the Grove BoosterPack. You can also do it without it but you will need to wire the Groves properly.
Using the Grove BoosterPack, connect to the UART connection which is pin 9 and 10 to the 4 Digit Display Grove. Then connect the Mini Fan Grove to pin 38 connector on the right side.
Connect the Mini Fan motor to the Mini Fan grove.
Upload the code and you are all set.
/*
Grove Mini Fan
This example shows how to demo a fan controller
Hardware Required:
* TI LaunchPad
* Grove Mini Fan
* Grove 4 Digit Display
* optional battery source
This example code is in the public domain.
*/
#include "TM1637.h"
int speed = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
#define CLK 9 //pins definitions for TM1637 and can be changed to other ports
#define DIO 10
TM1637 tm1637(CLK,DIO);
int8_t ZeroDisp[] = {
0x00,0x00,0x00,0x00};
int8_t SpeedDisp[] = {
0x00,0x00,0x00,0x00};
void setup() {
// declare pin 14 to be an output:
pinMode(38, OUTPUT);
tm1637.init();
tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
tm1637.point(POINT_OFF);
tm1637.display(ZeroDisp);
}
void loop() {
// set the speed of pin 38:
analogWrite(38, speed);
// change the speed for next time through the loop:
speed = speed + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (speed == 0 || speed == 255) {
fadeAmount = -fadeAmount ;
}
if(speed < 100){
SpeedDisp[0] = 0;
SpeedDisp[1] = (speed / 100);
SpeedDisp[2] = (speed / 10);
SpeedDisp[3] = (speed % 10);
}
else if(speed < 200 && speed >= 100) {
SpeedDisp[0] = 0;
SpeedDisp[1] = (speed / 100);
SpeedDisp[2] = ((speed - 100) / 10);
SpeedDisp[3] = (speed % 10);
}
else if(speed >= 200) {
SpeedDisp[0] = 0;
SpeedDisp[1] = (speed / 100);
SpeedDisp[2] = ((speed - 200) / 10);
SpeedDisp[3] = (speed % 10);
}
tm1637.display(SpeedDisp);
// wait for 300 milliseconds to see the changing speed effect
delay(300);
}
// Author:Frankie.Chu
// Date:9 April,2012
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
// Modified record:
//
/*******************************************************************************/
#ifndef TM1637_h
#define TM1637_h
#include <inttypes.h>
#include <Arduino.h>
//************definitions for TM1637*********************
#define ADDR_AUTO 0x40
#define ADDR_FIXED 0x44
#define STARTADDR 0xc0
/**** definitions for the clock point of the digit tube *******/
#define POINT_ON 1
#define POINT_OFF 0
/**************definitions for brightness***********************/
#define BRIGHT_DARKEST 0
#define BRIGHT_TYPICAL 2
#define BRIGHTEST 7
class TM1637
{
public:
uint8_t Cmd_SetData;
uint8_t Cmd_SetAddr;
uint8_t Cmd_DispCtrl;
boolean _PointFlag; //_PointFlag=1:the clock point on
TM1637(uint8_t, uint8_t);
void init(void); //To clear the display
void writeByte(int8_t wr_data);//write 8bit data to tm1637
void start(void);//send start bits
void stop(void); //send stop bits
void display(int8_t DispData[]);
void display(uint8_t BitAddr,int8_t DispData);
void clearDisplay(void);
void set(uint8_t = BRIGHT_TYPICAL,uint8_t = 0x40,uint8_t = 0xc0);//To take effect the next time it displays.
void point(boolean PointFlag);//whether to light the clock point ":".To take effect the next time it displays.
void coding(int8_t DispData[]);
int8_t coding(int8_t DispData);
private:
uint8_t Clkpin;
uint8_t Datapin;
};
#endif
// Author:Frankie.Chu
// Date:9 April,2012
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
// Modified record:
//
/*******************************************************************************/
#include "TM1637.h"
#include <Arduino.h>
static int8_t TubeTab[] = {0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};//0~9,A,b,C,d,E,F
TM1637::TM1637(uint8_t Clk, uint8_t Data)
{
Clkpin = Clk;
Datapin = Data;
pinMode(Clkpin,OUTPUT);
pinMode(Datapin,OUTPUT);
}
void TM1637::init(void)
{
clearDisplay();
}
void TM1637::writeByte(int8_t wr_data)
{
uint8_t i,count1;
for(i=0;i<8;i++) //sent 8bit data
{
digitalWrite(Clkpin,LOW);
if(wr_data & 0x01)digitalWrite(Datapin,HIGH);//LSB first
else digitalWrite(Datapin,LOW);
wr_data >>= 1;
digitalWrite(Clkpin,HIGH);
}
digitalWrite(Clkpin,LOW); //wait for the ACK
digitalWrite(Datapin,HIGH);
digitalWrite(Clkpin,HIGH);
pinMode(Datapin,INPUT);
while(digitalRead(Datapin))
{
count1 +=1;
if(count1 == 200)//
{
pinMode(Datapin,OUTPUT);
digitalWrite(Datapin,LOW);
count1 =0;
}
pinMode(Datapin,INPUT);
}
pinMode(Datapin,OUTPUT);
}
//send start signal to TM1637
void TM1637::start(void)
{
digitalWrite(Clkpin,HIGH);//send start signal to TM1637
digitalWrite(Datapin,HIGH);
digitalWrite(Datapin,LOW);
digitalWrite(Clkpin,LOW);
}
//End of transmission
void TM1637::stop(void)
{
digitalWrite(Clkpin,LOW);
digitalWrite(Datapin,LOW);
digitalWrite(Clkpin,HIGH);
digitalWrite(Datapin,HIGH);
}
//display function.Write to full-screen.
void TM1637::display(int8_t DispData[])
{
int8_t SegData[4];
uint8_t i;
for(i = 0;i < 4;i ++)
{
SegData[i] = DispData[i];
}
coding(SegData);
start(); //start signal sent to TM1637 from MCU
writeByte(ADDR_AUTO);//
stop(); //
start(); //
writeByte(Cmd_SetAddr);//
for(i=0;i < 4;i ++)
{
writeByte(SegData[i]); //
}
stop(); //
start(); //
writeByte(Cmd_DispCtrl);//
stop(); //
}
//******************************************
void TM1637::display(uint8_t BitAddr,int8_t DispData)
{
int8_t SegData;
SegData = coding(DispData);
start(); //start signal sent to TM1637 from MCU
writeByte(ADDR_FIXED);//
stop(); //
start(); //
writeByte(BitAddr|0xc0);//
writeByte(SegData);//
stop(); //
start(); //
writeByte(Cmd_DispCtrl);//
stop(); //
}
void TM1637::clearDisplay(void)
{
display(0x00,0x7f);
display(0x01,0x7f);
display(0x02,0x7f);
display(0x03,0x7f);
}
//To take effect the next time it displays.
void TM1637::set(uint8_t brightness,uint8_t SetData,uint8_t SetAddr)
{
Cmd_SetData = SetData;
Cmd_SetAddr = SetAddr;
Cmd_DispCtrl = 0x88 + brightness;//Set the brightness and it takes effect the next time it displays.
}
//Whether to light the clock point ":".
//To take effect the next time it displays.
void TM1637::point(boolean PointFlag)
{
_PointFlag = PointFlag;
}
void TM1637::coding(int8_t DispData[])
{
uint8_t PointData;
if(_PointFlag == POINT_ON)PointData = 0x80;
else PointData = 0;
for(uint8_t i = 0;i < 4;i ++)
{
if(DispData[i] == 0x7f)DispData[i] = 0x00;
else DispData[i] = TubeTab[DispData[i]] + PointData;
}
}
int8_t TM1637::coding(int8_t DispData)
{
uint8_t PointData;
if(_PointFlag == POINT_ON)PointData = 0x80;
else PointData = 0;
if(DispData == 0x7f) DispData = 0x00 + PointData;//The bit digital tube off
else DispData = TubeTab[DispData] + PointData;
return DispData;
}
Comments