Showing posts with label LEDs. Show all posts
Showing posts with label LEDs. Show all posts
Monday, October 5, 2020
STM32 LED Color Matching For Kids
Fun activity for kids with simple LED circuit! The LEDs will light up when the corresponding connectors of the same color are matching.
[フレーム]
STM32 Blue Pill (STM32F103C8T6-development-board) circuit connections:
STM32CubeIDE Project:
main.c:
Saturday, May 6, 2017
ATtiny IR Remote
ATtiny2313 Infrared Sender
parts:
avr-gcc (main.c) code:
References:
ESP8266 IR Remote
z3t0/Arduino-IRremote
ATtiny Low Power
parts:
- ATtiny2313 @4MHz (internal oscillator)
- IR LED (series with 220-ohm resistor)
- Tact switches
- 3.7V lithium ion battery & Capacitor
avr-gcc (main.c) code:
References:
ESP8266 IR Remote
z3t0/Arduino-IRremote
ATtiny Low Power
Sunday, January 29, 2017
WiFi RGB Matrix
Here's a DIY flexible 36x8 RGB-LED-matrix controlled via WiFi connection using an ESP8266 module.
[フレーム]
Arduino Code: julznc/wifi_rgb_matrix
Assembly:
References:
NeoPixelBus - for interfacing WS2812 LEDs to ESP8266 controller
Adafruit-GFX-Library - with font library for displaying texts
LED Matrix Studio - for creating bitmap images
[フレーム]
Arduino Code: julznc/wifi_rgb_matrix
Assembly:
References:
NeoPixelBus - for interfacing WS2812 LEDs to ESP8266 controller
Adafruit-GFX-Library - with font library for displaying texts
LED Matrix Studio - for creating bitmap images
Monday, May 9, 2016
Android-Arduino Communication via USB OTG
USB On-the-go capability in Android devices has now become more available in the market. And why wouldn’t it be? This feature, nicknamed OTG, enables you to use your flash drives, keyboards, mice and even printers with just your phone! What’s more interesting is it can also enable you to communicate and control your microprocessors using your Android device without the need for additional modules – just a cable. In this article, we will see how this communication can become possible. To demonstrate, we will control the behavior of an LED and send messages to another very popular item in the electronics world – the Arduino.
The shorter end of the stick is Arduino’s side. In your Arduino, simply upload this code:
int ledPin = 13; void setup(){ Serial.begin(9600); Serial.setTimeout(200); pinMode(ledPin, OUTPUT); } void loop(){ if (Serial.available()){ String c = Serial.readString(); if (c.equals("TONLED")) digitalWrite(ledPin, HIGH); else if (c.equals("TOFFLED")) digitalWrite(ledPin, LOW); else Serial.print(c); } }
In the above sketch, we are simply waiting for the data arriving at our serial line and performing actions based on the data received. For instance, turning on the LED ledPin requires a TONLED message from our Android device. You’ve probably noticed that there are no special libraries or methods in our Arduino sketch. That’s a great thing because it tells us that the system is not exclusive to Arduino and will work with any microcontroller that supports serial communication.
Let’s now move on to Android’s side. The first step is to create an Android project and add the necessary components. In the project we created, we added extra components for user convenience. For learning and testing purposes, only the following are necessary:
- Text Field – used to get input data by the user, which will be sent to and echoed by the Arduino
- Toggle Button – used to control the behavior of the LED
- Start Button – used to open the serial port
- Send Button – used to send messages to Arduino
- Text View – used to display logs
To simplify the setup and processes, we will use the UsbSerial library by felHR85. There are a lot of libraries you can choose from. In case you have other preferences, feel free to modify and adapt to your preferred library.
In the build.gradle of your project, add jitpack. Jitpack is a very awesome tool that enables us to get a Git project into our build.
allprojects { repositories { jcenter() maven { url "https://jitpack.io" } } }
Now, add the dependency to your module’s build.gradle.
compile 'com.github.felHR85:UsbSerial:4.3'
Moving on to our main activity, there are some variables that we wish to declare globally for convenience.
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"; UsbDevice device; UsbDeviceConnection connection; UsbManager usbManager; UsbSerialDevice serialPort; PendingIntent pendingIntent;
The next items that we will present here will not be discussed thoroughly, but you can refer to Android's official documentation for details.
Before trying to start the communication, you must seek permission from the user. To do this, create a broadcast receiver. This receiver listens for the intent that gets broadcasted when you call requestPermission(). Only when granted can we proceed to opening the connection and setting parameters for the Serial communication.
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ACTION_USB_PERMISSION)) { boolean granted = intent.getExtras().getBoolean(UsbManager.EXTRA_PERMISSION_GRANTED); if (granted) { connection = usbManager.openDevice(device); serialPort = UsbSerialDevice.createUsbSerialDevice(device, connection); if (serialPort != null) { if (serialPort.open()) { serialPort.setBaudRate(9600); serialPort.setDataBits(UsbSerialInterface.DATA_BITS_8); serialPort.setStopBits(UsbSerialInterface.STOP_BITS_1); serialPort.setParity(UsbSerialInterface.PARITY_NONE); serialPort.setFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF); serialPort.read(mCallback); } else { Log.d("SERIAL", "PORT NOT OPEN"); } } else { Log.d("SERIAL", "PORT IS NULL"); } } else { Log.d("SERIAL", "PERMISSION NOT GRANTED"); } } else if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) { onClickStart(startButton); } else if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) { //can add something to close the connection } }; };
On your onCreate method, declare the intent and register your broadcast receiver to start and stop the serial connection.
pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); registerReceiver(broadcastReceiver, filter);
In our application, we created a start button to start the connection when pressed. In the method that corresponds to the onClick action of our button, we add the following:
public void onClickStart(View view) { if (!isSerialStarted) { usbManager = (UsbManager) getSystemService(Context.USB_SERVICE); HashMap<String, UsbDevice> usbDevices = usbManager.getDeviceList(); if (!usbDevices.isEmpty()) { boolean keep = true; for (Map.Entry<String, UsbDevice> entry : usbDevices.entrySet()) { device = entry.getValue(); int deviceVID = device.getVendorId(); if (deviceVID == 1027 || deviceVID == 9025) { //Arduino Vendor ID usbManager.requestPermission(device, pendingIntent); keep = false; } else { connection = null; device = null; } if (!keep) break; } } } }
The code above searches for vendor IDs 1027 or 9025 – the vendor ID’s associated to FTDI or Arduino. The vendor ID equal to 9025 is the more popular and more common value based on other articles in the internet, but mine has an ID of 1027. The easiest way to know is to just print the vendor IDs detected by the Android device. If the vendor ID matches the expected ID for our device, we will call the requestPermission() method. With this, the intent will be broadcasted and picked up by our receiver, starting and opening the connection.
Once communication is opened, we can start sending and receiving data. To receive from Arduino, simply add the codes below. Note that we are appending the data received to the text view.
private UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() { //Defining a Callback which triggers whenever data is read. @Override public void onReceivedData(byte[] arg0) { String data = null; try { data = new String(arg0, "UTF-8"); data.concat("/n"); tvAppend(displayView, data); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }; private void tvAppend(final TextView tv, final CharSequence text) { runOnUiThread(new Runnable() { @Override public void run() { if (text != null) { tv.append(text); } } }); }
Sending data is easier. We only need to get user input from the text field, and send it to the connected device.
public void onClickSend(View view) { String textInput = inputView.getText().toString(); serialPort.write(textInput.getBytes()); }
To control the LED in Arduino, simply add the code below. You are free to change TONLED and TOFFLED to whatever names you want. Just don’t forget to adjust the Arduino code as well.
public void onClickToggle(View view) { if (isLedON == false) { isLedON = true; tvAppend(displayView, "\nLED TURNED ON\n"); serialPort.write("TONLED".getBytes()); } else { isLedON = false; serialPort.write("TOFFLED".getBytes()); tvAppend(displayView, "\nLED TURNED OFF\n"); } }
You can close the connection using:
serialPort.close();
We are almost done. In your manifest file, add the following so that your application will be notified of an attached USB device.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="yourpackage.com.name"> <uses-feature android:name="android.hardware.usb.host" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> </intent-filter> <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" /> <intent-filter> <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" /> </intent-filter> </activity> </application> </manifest>
Create an xml folder inside the res folder and add device_filter.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <usb-device vendor-id="9025"/> </resources>
And… were done! Additional tips, we can add a checker to confirm that the serial connection is already open. This saves us from crashes due to attempts to send serial data while the connection is still closed. We can also add clear buttons, or place the text view inside a scroll view then automatically scroll to end of page using:
mScrollView.smoothScrollTo(0, displayView.getBottom());
That’s it. If you want to extend your phone’s sensors, or if you want to add storage, wireless and messaging capability, camera and orientation sensors in your microprocessor project with just one device, USB On-the-Go, may be your way to go.
A demo application, SimpleArDroid by YMSoftLabs can be downloaded from Google Play. Here's a video of how the system works.
[フレーム]
References:
UsbSerial: A serial port driver library for Android v3.0
USBSerial
Communicate with Your Arduino Through Android
Friday, September 18, 2009
Spinning LEDs Display
Spinning LEDs Display using PIC16LF73 (SSOP) and 7 SMT LEDs:
complete assembly and synchronizing mechanism:
demo:
[埋込みオブジェクト:http://www.youtube.com/v/UZuYfuCvn-w&hl=en&fs=1]
additional info: Another Spinning LED Display
complete assembly and synchronizing mechanism:
demo:
[埋込みオブジェクト:http://www.youtube.com/v/UZuYfuCvn-w&hl=en&fs=1]
additional info: Another Spinning LED Display
Thursday, September 17, 2009
mini LCD Projector
simple LCD projector using a small LCD and a bright white LED.
using Nokia 6610/6100 colored LCD + OSRAM high-power LED
demo video:[埋込みオブジェクト:http://www.youtube.com/v/xOBEpLd1A6w&hl=en&fs=1&]
using Nokia 6610/6100 colored LCD + OSRAM high-power LED
demo video:[埋込みオブジェクト:http://www.youtube.com/v/xOBEpLd1A6w&hl=en&fs=1&]
Wednesday, September 16, 2009
POV/BCD Clock
POV(persistence of vision)/BCD(binary coded decimal) Clock
*POV mode:
*BCD mode:
source code for PIC16F microcontroller:
*POV mode:
*BCD mode:
source code for PIC16F microcontroller:
/////////////////////////////////////////////////////
// Persistence Of Vision (POV) clock
// Binary Coded Decimal (BCD) clock
// Hi-Tech C code by YUS of ElectronicsLab.Ph
// revision 2.1 Nov 24, 2008
/////////////////////////////////////////////////////
#include <htc.h>
#define _XTAL_FREQ 32768
__CONFIG(XT & WDTDIS & PWRTEN & BORDIS & DUNPROT & WRTEN & UNPROTECT);
#define leds PORTC
#define leds_tris() TRISC = 0x00;
#define adjust RB7
#define adjust_minutes RB6
#define adjust_hours RB5
const unsigned char digits[10][5]={ //10 digits, 5 bytes each
0xc1,0xae,0xb6,0xba,0xc1, // 0
0xff,0xbd,0x80,0xbf,0xff, // 1
0xbd,0x9e,0xae,0xb6,0xb9, // 2
0xde,0xbe,0xba,0xb4,0xce, // 3
0xe7,0xeb,0xed,0x80,0xef, // 4
0xd8,0xba,0xba,0xba,0xc6, // 5
0xc3,0xb5,0xb6,0xb6,0xcf, // 6
0xfe,0x8e,0xf6,0xfa,0xfc, // 7
0xc9,0xb6,0xb6,0xb6,0xc9, // 8
0xf9,0xb6,0xb6,0xd6,0xe1,}; // 9
/*
const unsigned char am_pm[2][11]={
0x81,0xee,0xee,0xee,0x81,0xff,0x80,0xfd,0xf3,0xfd,0x80, // AM
0x80,0xf6,0xf6,0xf6,0xf9,0xff,0x80,0xf6,0xf6,0xf6,0xf9,}; // PM
*/
unsigned char hours, minutes, seconds;
//unsigned char hours_tens, hours_ones, minutes_tens, minutes_ones;
// Interrupt Handler
void interrupt isr()
{
// Timer0 Interrupt - Freq = 0.50 Hz - Period = 2.000000 seconds
if (TMR0IF) // timer 0 interrupt flag
{
TMR0IF = 0; // clear the flag
seconds += 2; //increment time
}
}
void initialize(void)
{
//initial time
hours = 12;
minutes = 34;
seconds = 0;
//Timer0 Registers Prescaler= 64 - TMR0 Preset = 0 - Freq = 0.50 Hz - Period = 2.000000 seconds
T0CS = 0; // bit 5 TMR0 Clock Source Select bit...0 = Internal Clock (CLKO) 1 = Transition on T0CKI pin
T0SE = 0; // bit 4 TMR0 Source Edge Select bit 0 = low/high 1 = high/low
PSA = 0; // bit 3 Prescaler Assignment bit...0 = Prescaler is assigned to the Timer0
PS2 = 1; // bits 2-0 PS2:PS0: Prescaler Rate Select bits
PS1 = 0;
PS0 = 1;
TMR0 = 0; // preset for timer register
// Interrupt Registers
INTCON = 0; // clear the interrpt control register
TMR0IE = 1; // bit5 TMR0 Overflow Interrupt Enable bit...1 = Enables the TMR0 interrupt
TMR0IF = 0; // bit2 clear timer 0 interrupt flag
GIE = 1; // bit7 global interrupt enable
leds_tris();
RBPU = 0; //enable portB internal pullups
}
void leds_on(unsigned char char_byte) //0xff = all LEDs off; 0x00 = all LEDs on
{
leds = char_byte; // use port C for LEDs
NOP();
}
void check_time(void)
{
if (seconds >= 60) { seconds-=60; minutes+=1; }
if (minutes >= 60) { minutes-=60; hours+=1; }
if (hours > 12) { hours -=12; } //12-hour format
//if (hours > 24) { hours -=24; } //24-hour format
}
void pov_display(void) // persistence of vision (POV) mode
{
unsigned char i, digit, hours_tens[5], hours_ones[5], minutes_tens[5], minutes_ones[5];
digit = hours/10;
for(i=0; i<5; i++) hours_tens[i]= digits[digit][i];
digit = hours%10;
for(i=0; i<5; i++) hours_ones[i]= digits[digit][i];
digit = minutes/10;
for(i=0; i<5; i++) minutes_tens[i]= digits[digit][i];
digit = minutes%10;
for(i=0; i<5; i++) minutes_ones[i]= digits[digit][i];
if ((hours/10)!=0) //'wag ng i-display kung wala naman
{
// hours' tens digit
leds_on(hours_tens[0]);
leds_on(hours_tens[1]);
leds_on(hours_tens[2]);
leds_on(hours_tens[3]);
leds_on(hours_tens[4]);
}
// hours' ones digit
leds_on(hours_ones[0]);
leds_on(hours_ones[1]);
leds_on(hours_ones[2]);
leds_on(hours_ones[3]);
leds_on(hours_ones[4]);
leds_on(0xff);
_delay(5);
// colon
leds_on(0xc9);
_delay(5);
leds_on(0xff);
// minutes' tens digit
leds_on(minutes_tens[0]);
leds_on(minutes_tens[1]);
leds_on(minutes_tens[2]);
leds_on(minutes_tens[3]);
leds_on(minutes_tens[4]);
leds_on(0xff);
_delay(2);
// minutes' ones digit
leds_on(minutes_ones[0]);
leds_on(minutes_ones[1]);
leds_on(minutes_ones[2]);
leds_on(minutes_ones[3]);
leds_on(minutes_ones[4]);
leds_on(0xff);
}
void binary_display(void) //binary coded decimal (BCD) mode
{
leds_on(~( ( (hours/10)<<4) + (hours%10) ) ); // BCD hours
__delay_ms(1000);
leds_on(0xff);
__delay_ms(200);
leds_on(~( ( (minutes/10)<<4) + (minutes%10) ) ); //BCD minutes
__delay_ms(1000);
leds_on(0xff);
__delay_ms(500);
}
void adjust_time(void)
{
TMR0IE = 0; //disable timer0interrupt
while( adjust_hours && adjust_minutes ) binary_display();
while(!adjust)
{
// with BCD display
if(!adjust_minutes)
{
minutes+=1;
check_time();
leds_on(~( ( (minutes/10)<<4) + (minutes%10) ) ); //BCD minutes
__delay_ms(500);
}
if(!adjust_hours)
{
hours+=1;
check_time();
leds_on(~( ( (hours/10)<<4) + (hours%10) ) ); // BCD hours
__delay_ms(500);
}
}
TMR0IE = 1; //enable timer0 interrupt
}
void main()
{
initialize();
while(1)
{
check_time();
if(!adjust) adjust_time();
//choose either pov or binary mode
pov_display(); __delay_ms(200);
//binary_display(); __delay_ms(200);
}
}
Subscribe to:
Comments (Atom)