Teensy as Serial device - teensy

I am using teensy 3.2 to program its K20 series microcontroller Mk20DX256
I have written a simple UART code to transmit a character 'U' from teensy to PC and see it on Docklight.I have disabled interrupt and DMA
I have searched through web and found out that Teensy acts as Serial port only when Serial type code is running
Otherwise it acts as USB Port
I uploaded 'hello' example from arduino to board and chose Usb serial port
This prints hello on serial monitor.the device is detected in COM PORT in hardware manager
But when i try to program my hex file generated from keil into the board
The Com port option disappers from Port in hardware manager
It means my code is not serial type
#include "MK20D7.h"
void UARTPutChar(char);
int main()
{
SIM->SCGC4=1UL<<10; //Clock Enable Uart0
SIM->SCGC5 =1UL<<12;//Enable GPIO PORT A clock
UART0->C2=0X00; //Disable UART
PORTD->PCR[7]= 0x00000300; //Port pin declare as UART0TX
UART0->BDH=0x01; //
UART0->BDL=0xD4;
UART0->C1=0X00;
UART0->C3=0X00;
UART0->S2=0X20;
UART0->S1=0X40;
UART0->C2=0X08; //Enable UART0
while(1)
{
UARTPutChar('U');
}
}
void UARTPutChar(char ch)
{
if(UART0->S1 == 0X40)
{
UART0->D=ch;
}
}
Anyone please guide what i am doing wrong.

If you compile for Teensy with Arduino, the build system will link in the Teensyduino core library which contains a working USB stack. Specifically, per default it will configure it as a CDC device (virtual serial port).
If you compile with Keil, I assume that you need to tell the IDE that you want to link in a USB stack and how you want it configured

Related

How can I connect two BLE modules to eachother?

Hi i would to connect two Arduino with a BLE module, but i don't know what module can i use and how to use it. I know that bluetooth connection is based on master slave relation, but when one is master how can i search the other BLE module to connect and then how can i connect the two modules?
I've worked with Bluetooth some to connect to an Android, but not BLE or between 2 Arduinos. However, I did find some articles that should provide some guidance that makes sense to me.
Your BLE modules should connect the same way as BT2 modules. I suspect BLE will become the norm before too long.
The AT codes are the same for both the HC-05 and the HM-10 and the latter should be a drop-in replacemnt for the former. In the light of this, the article by Philipe Cantin on Arduino<>ARduino connection should apply with BLE.
You need modules that are in master mode. While master modules are in slave mode by default, they can all be set up as master. I am not aware of any BLE module that is slave-only.
http://phillipecantin.blogspot.com.au/2014/08/hc-05-bluetooth-link-with-zero-code.html
Note that, if power is a concern, both modules need to be BLE.
https://forum.arduino.cc/index.php?topic=358570.0
In the Connecting 2 Arduinos by Bluetooth using a HC-05 and a HC-06: Pair, Bind, and Link post I explained how to connect a HC-05 to a HC-06 so that when powered they automatically made a connection. Here we look at using that connection to get Arduinos talking over Bluetooth.
http://www.martyncurrey.com/connecting-2-arduinos-by-bluetooth-using-a-hc-05-and-a-hc-06-pair-bind-and-link/
Most HC-05s and HC-06s have 3.3v TX and RX pins. 5V Arduinos will read 3.3v as HIGH so the BT modules TX pin can be connected directly to the Arduino RX pin. However, the Arduino TX pin needs to be converted to 3.3v before connecting to the BT modules RX pin. A simple way to do this is by using a voltage divider made from 2 resistors; I generally use 1 x 1K and 1 x 2K.
Arduino RX (pin 8) to BT module TX pin
Arduino TX (pin 9) to BT module RX pin via a voltage divider
Both Arduinos have the same connections to the BT modules.
* Sketch: Arduino2Arduino_MASTER_01
* By Martyn Currey
* 08.04.2016
* Written in Arduino IDE 1.6.3
*
* Send commands through a serial connection to turn a LED on and OFF on a remote Arduino
* There is no error checking and this sketch sends only
* Commands should be contained within the start and end markers < and >
*
* D8 - AltSoftSerial RX
* D9 - AltSoftSerial TX
*
*/
// AltSoftSerial uses D9 for TX and D8 for RX. While using AltSoftSerial D10 cannot be used for PWM.
// Remember to use a voltage divider on the Arduino TX pin / Bluetooth RX pin
// Download AltSoftSerial from https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
#include <AltSoftSerial.h>
AltSoftSerial BTserial;
// Change DEBUG to true to output debug information to the serial monitor
boolean DEBUG = true;
void setup()
{
if (DEBUG)
{
// open serial communication for debugging and show
// the sketch filename and the date compiled
Serial.begin(9600);
Serial.println(__FILE__);
Serial.println(__DATE__);
Serial.println(" ");
}
// open software serial connection to the Bluetooth module.
BTserial.begin(9600);
if (DEBUG) { Serial.println("BTserial started at 9600"); }
} // void setup()
void loop()
{
BTserial.println("<LEDON>");
if (DEBUG) {Serial.println("LEDON command sent");}
delay (1000);
BTserial.println("<LEDOFF>");
if (DEBUG) {Serial.println("LEDOFF command sent");}
delay (1000);
}
http://www.martyncurrey.com/arduino-to-arduino-by-bluetooth/

Interfacing a USB/serial device with Arduino USB/serial interface

On one side I have a device with a USB (FTDI chip) interface communicating in serial 9600bps,N,8,1 - the default configuration for the Arduino USB/serial interface.
On the other side I have a simple Arduino sketch that starts a serial session and transmits data.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600,SERIAL_8N1);
}
void loop() {
// put your main code here, to run repeatedly:
char* data_to_send="66";
SSEND(data_to_send);
delay(5000);
}
String SSEND(char* data){
String protocol="AT$SF=";
protocol+=(String)data;
// protocol+="\r";
Serial.println(String(protocol));
delay(1000);
return "OK";
}
The sketch works just fine when connected to the computer.
Then I try to connect to the device and I see the Tx LED that stops blinking so it doesn't send anything and of course the device doesn't work like expected. Besides, I tried sending serial commands directly from the computer to the device and it works just fine.
So my questions are:
Why does the serial interface between Arduino and my device doesn't work?
Why does Arduino stop sending data once the USB/serial interface switched from the computer to the device
What would be the solution to make the device work with the Arduino?
Should I switch the TX & RX with a splitted FTDI cable plugged to port 0 and 1?
Thanks for your help
I suspect that device is a USB device and not a USB host, and you plugged two USB devices together. That Arduino is not a USB host, and a USB connection always needs a host.
The plug adapter you're using is not even supposed to exist according to the USB spec, because the different shapes of plugs are specifically there to make it impossible to plug two devices into each other like you've done here.
The way to make it work would be to use another board that actually supports acting as a USB host.

Serial Communication over Desktop to Arduino

I have a USB 2 Serial adapter and the device is working fine. I see the device perfectly configured in my System.
I have connected the TX0 pin of Arduino to DB 2 pin (read pin) of the adapter. Below is my Arduino code:
int i = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
for(i=0;i<6;i++)
{
Serial.write('H');
delay(100);
}
}
But when I try to read thru terminal software of data received at my COM port I see some junk character incoming. I am pretty sure that I am using same baud rate / flow setting both side. Why am I facing this issue - do I need to connect any other pins also as I just need to receive data at system side?
You should connect the ground pin with the ground pin
Arduino Communicate with Ur computer using pins 0 and 1( Tx0 and Rx0)
you shouldn't be connected to the Tx0 pin to another serial device because arduino use it to communicate with computer.
if you are using Uno. Check for Software Serial. and two grounds should be connected.

Arduino Serial1

I'm using an Arduino Micro. When I use "Serial.write", etc. with the Arduino's IDE serial monitor, everything is working fine.
However, when I try to read or send data via "Serial1", I get nothing. "Serial1" is supposed to use 0 and 1, RX and TX, respectively.
Do I need to connect these pins through a USB converter or are they connected on the boards USB converter?
Here is the code:
Void setup(){ Serial1.begin(4800); }
Void loop(){ Serial1.prrint('X'); }
The only serial port connected to the USB that the serial monitor can read from is Serial.
Serial1, Serial2, and Serial3 are all logic level serial and will not show up on the Arduino serial monitor.
If you want to see the output from these on your computer, it will require extra hardware.
Serial is the only serial port connected to USB. So serial monitor can access only that port. If you need Serial1 or Serial2 to be accessed by serial monitor, then you should use 'USB to TTL Serial Cable' and connect this to RX and TX pins of the arduino's Serial1 port.
Please visit link for USB to TTL Serial Cable, enter link description here
"Serial1" in Arduino Micro is physically connected to the TX and RX pins (TTL), and "Serial" is just a "virtual port" which you can read using Arduino IDE's Serial Monitor. That’s why Arduino Micro is a little different from another, such as Arduino Nano or Arduino Pro Mini.
If you use Serial and Serial1, you can approach this advantage, upload code using USB and make a connection through Bluetooth (using HC06 connected to physical pins) without disconnecting the USB cable and powered both devices (Arduino Micro and Bluetooth).
If you can't upload code to your Arduino Micro sometimes, press the Arduino Micro's reset button, release it, and press the upload button in Arduino IDE's.
"virtual port" sometimes needs to restart and connect using USB.
This is from Arduino's documentation website:
...Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data using the ATmega32U4 hardware serial capability. Note that on the Micro, the Serial class refers to USB (CDC) communication; for TTL serial on pins 0 and 1, use the Serial1 class.
You said it right. Serial1 is the RX and TX pin, while Serial is a virtual interface between the computer and Arduino. I have used the TX and RX pins for a wireless module, and if you need to use Serial1, it would have to occupy pins 0 and 1, and switch from DLINE to UART on your board.
Make sure you go to tool/board: and select Arduino Mega (or other board with multiply serial ports) or it won't work, because the Uno only has one Serial communication port (aka The TX and RX pins on pins on 1 and 0)! Write 1,2 or 3 depending on what TX and RX pins you are using on the Board. The mega has a whole set of extra pins for Serial 1,2 and 3, for example:
Arduino Uno (etc):
Serial.begin(9600)
Serial.write("testing")
Arduino Mega:
Serial1.begin(9600) // <{or what even baud rate you should use}
Serial1.write("testing")
or
Serial2.begin(9600)
Serial2.write("testing")
or
Serial3.begin(9600)
Serial3.write("testing")
Serial1 is the wrong class for pin 0 and pin 1. You should use Serial class.
Do I need to connect these pins through a USB converter or are they connected on the boards USB converter?
It makes no difference for Serial class.
Use:
Void setup()
{
Serial.begin(4800); // 9600....
}
void loop()
{
if(Serial.available())
{
int a = Serial.read();
Serial.Writeln(a);
}
else
{
Serial.Writeln("Error");
}
}
Open the serial monitor with the icon placed in right corner of Arduino IDE. It will be available if you connect the Arduino with PC.
When you open the Arduino IDE, write this code block:
Void setup()
{
Serial.begin(9600);
}
void loop()
{
if(Serial.available())
{
char get = Serial.read();
Serial.Write(get);
}
}
Select the Arduino 9600 port and write something. If you get your written text, your Arduino is ready for serial communication.
You have to define Serial1 by using SoftwareSerial class from SoftwareSerial library, google and download the library:
The code should be something like this:
// Example
SoftwareSerial Serial1(9, 10); // RX and TX, respectively
Void setup() {
Serial1.begin(4800); // Here is your New serial
Serial.begin(9600); // This is where Arduino is connected to your PC
}
Void loop() {
// Code goes Here
}

Bluetooth RN41 does not respond

I want to send some commands to my RN41 Bluetooth Module connecting to Arduino Leonardo over the serial port using serial monitor, as the tutorial shows. But it does not respond. I can connect to the bluetooth modul and the status LED blinks right. I tried to send $$$ to change to command mode, and the blink rate does change to 10/sec, but module responds nothing. And when I send '---', the blink rate back to normal. I think it means the connection is successful but I just cannot see anything at serial monitor.
I set monitor's baud to 9600, as exactly the tutorial shows. (https://learn.sparkfun.com/tutorials/using-the-bluesmirf/example-code-using-command-mode)
Do you guys know what could be wrong?
Code attached:
/*
Example Bluetooth Serial Passthrough Sketch
by: Jim Lindblom
SparkFun Electronics
date: February 26, 2013
license: Public domain
This example sketch converts an RN-42 bluetooth module to
communicate at 9600 bps (from 115200), and passes any serial
data between Serial Monitor and bluetooth module.
*/
#include <SoftwareSerial.h>
int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
Serial.begin(9600); // Begin the serial monitor at 9600bps
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
bluetooth.print("$"); // Print three times individually
bluetooth.print("$");
bluetooth.print("$"); // Enter command mode
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth.begin(9600); // Start bluetooth serial at 9600
}
void loop()
{
if(bluetooth.available()) // If the bluetooth sent any characters
{
// Send any characters the bluetooth prints to the serial monitor
Serial.print((char)bluetooth.read());
}
if(Serial.available()) // If stuff was typed in the serial monitor
{
// Send any characters the Serial monitor prints to the bluetooth
bluetooth.print((char)Serial.read());
}
// and loop forever and ever!
}
I was stuck on very simple case:
To enter command mode you have to send $$$ without any CR/LF.
after you entered command mode you have to send commands, and every command has to be followed by CR LF. if not - module will not response.
Hope that helps.
Yes, send only 3 characters, "$$$". I was also stuck for a bit. I also found that reading the Mate response "CMD" is necessary, which is not shown in the published sketch.
I've got some brandnew RN41VX modules.
I connect them by a XBEE Explorer USB module (almost same as RN41 EVAL Kit) to computer.
Using a terminal with 115 kbaud I send $$$ (without any trailing chars as 0x0d) to get into command mode. The LED switches to 10Hz blinhking - all fine.
But no respone appears in terminal.
Solution:
I had to switch on RTS Signal, even if manual tells "Flow Control: none"
kind regards Volker

Resources