HC-05 ⸮ In Serial Does Not Function - c

I recently got a HC-05 Bluetooth module for my arduino, but I cannot send or receive data from it. I used a code to turn on or off a led, but after I send a character from the Serial Monitor of my PC, I get ⸮. Also the module does not respond to any AT command. HC-05 ConnectionArduino connection I ran the Serial both in 9600 and 38400 baud but nothing changed. Also I have tried both no line ending and both NL and CR. But is wrong with this module? Here is my code:
/*
Arduino Turn LED On/Off using Serial Commands
Created April 22, 2015
Hammad Tariq, Incubator (Pakistan)
It's a simple sketch which waits for a character on serial
and in case of a desirable character, it turns an LED on/off.
Possible string values:
a (to turn the LED on)
b (tor turn the LED off)
*/
char junk;
String inputString="";
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set the baud rate to 9600, same should be of your Serial Monitor
pinMode(13, OUTPUT);
}
void loop()
{
if(Serial.available()){
while(Serial.available())
{
char inChar = (char)Serial.read(); //read the input
inputString += inChar; //make a string of the characters coming on serial
}
Serial.println(inputString);
while (Serial.available() > 0)
{ junk = Serial.read() ; } // clear the serial buffer
if(inputString == "a"){ //in case of 'a' turn the LED on
digitalWrite(13, HIGH);
}else if(inputString == "b"){ //incase of 'b' turn the LED off
digitalWrite(13, LOW);
}
inputString = "";
}
}

I will go step by step-
The connection
Arduino Pins Bluetooth Pins
RX (Pin 0) ———-> TX
TX (Pin 1) ———-> RX
5V ———-> VCC
GND ———-> GND
Connect a LED negative to GND of arduino and positive to pin 13 with a resistance valued between 220Ω – 1KΩ. And your done with the circuit.
Note : Don’t Connect RX to RX and TX to TX of Bluetooth to arduinoyou will receive no data , Here TX means Transmit and RX means Receive.
/*
* This program lets you to control a LED on pin 13 of arduino using a bluetooth module
*/
char data = 0; //Variable for storing received data
void setup()
{
Serial.begin(9600); //Sets the baud for serial data transmission
pinMode(13, OUTPUT); //Sets digital pin 13 as output pin
}
void loop()
{
if(Serial.available() > 0) // Send data only when you receive data:
{
data = Serial.read(); //Read the incoming data & store into data
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n");
if(data == '1') // Checks whether value of data is equal to 1
digitalWrite(13, HIGH); //If value is 1 then LED turns ON
else if(data == '0') // Checks whether value of data is equal to 0
digitalWrite(13, LOW); //If value is 0 then LED turns OFF
}
}
Link to Connection : https://halckemy.s3.amazonaws.com/uploads/image_file/file/153200/hc-05-LED%20blink%20Circuit.png
NOTE : While uploading the code , remove the TX and RX wires of Bluetooth Module from Arduino, once upload is completed, connect them.

#include <SoftwareSerial.h>
SoftwareSerial hc(2, 3); // RX | TX
void setup()
{
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
Serial.begin(9600);
Serial.println("Enter AT commands:");
hc.begin(38400); // HC-05 default speed in AT command more
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (hc.available())
Serial.write(hc.read());
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available())
hc.write(Serial.read());
}
Use this code to test the bluetooth module in command mode.there are two modes in hc-05. one is command mode and other is data mode.
Press the button which is on bluetooth module for few sec. then the led Toggles slowly at this point the module is in command mode and in this you can test the AT commands.
Note: Open the serial monitor in 9600 baud rate

Related

Raspberry pi and arduino CAN bus communication not working

So I am working on a project to establish CAN bus communication between raspberry pi 4 and arduino. Unfortunately, I don't have great success. So I already established connection between multiples raspberry pies and it is working perfectly. So to configure my raspberry pi I followed the steps of online tutorial. I added this lines in the /boot/config.txt:
dtoverlay=mcp2515-can0,oscillator=8000000,interupt=12
dtoverlay=spi-bcm2835-overlay
installed the can utilities and initiate the can using
sudo ip link set can0 up type can bitrate 500000
After that I created python code to control the receiving and sending of data to the CAN bus. Here is my code for sending data:
import time
import can
bustype = 'socketcan'
channel = 'can0'
bus = can.interface.Bus(channel=channel, bustype=bustype,bitrate=500000)
while True:
msg = can.Message(arbitration_id=2, data=[0, 0, 0, 0, 0, 0], is_extended_id=False)
bus.send(msg)
time.sleep(0.1)
</code></pre>
and here's is the one for receiving:
import time
import can
bustype = 'socketcan'
channel = 'can0'
bus = can.interface.Bus(channel=channel, bustype=bustype,bitrate=500000)
while True:
print(bus.recv())
time.sleep(0.1)
So for the arduino part I tried using UNO and Mega2560.
My connection on both boards is the same:
VCC --> 5V
GND --> GND
CS --> pin 10
SO --> pin12
SI --> pin11
SCK --> pin 13
INT --> pin 2
Tried multiple libraries on of them https://github.com/Seeed-Studio/Seeed_Arduino_CAN/blob/master/examples/recv_sd/recv_sd.ino
and the https://www.arduinolibraries.info/libraries/can. So I uploaded the example code to the arduino. Here it is:
#include
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("CAN Sender");
// start the CAN bus at 500 kbps
// if (!CAN.begin(500E3)) {
// Serial.println("Starting CAN failed!");
// while (1);
// }
}
void loop() {
// send packet: id is 11 bits, packet can contain up to 8 bytes of data
Serial.print("Sending packet ... ");
CAN.beginPacket(0x12);
CAN.write('h');
CAN.write('e');
CAN.write('l');
CAN.write('l');
CAN.write('o');
CAN.endPacket();
Serial.println("done");
delay(1000);
// send extended packet: id is 29 bits, packet can contain up to 8 bytes of data
Serial.print("Sending extended packet ... ");
CAN.beginExtendedPacket(0xabcdef);
CAN.write('w');
CAN.write('o');
CAN.write('r');
CAN.write('l');
CAN.write('d');
CAN.endPacket();
Serial.println("done");
delay(1000);
}
and when i upload it to the Arduino Uno it gives "Starting CAN failed!", but on the Arduino Mega everything seems to work fine, but both times no data is received by the raspberry pi no matter if i use the python code or simple "candump can0" command. Tried it even with the receiving example :
#include
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("CAN Receiver");
// start the CAN bus at 500 kbps
if (!CAN.begin(500E3)) {
Serial.println("Starting CAN failed!");
while (1);
}
}
void loop() {
// try to parse packet
int packetSize = CAN.parsePacket();
if (packetSize || CAN.packetId() != -1) {
// received a packet
Serial.print("Received ");
if (CAN.packetExtended()) {
Serial.print("extended ");
}
if (CAN.packetRtr()) {
// Remote transmission request, packet contains no data
Serial.print("RTR ");
}
Serial.print("packet with id 0x");
Serial.print(CAN.packetId(), HEX);
if (CAN.packetRtr()) {
Serial.print(" and requested length ");
Serial.println(CAN.packetDlc());
} else {
Serial.print(" and length ");
Serial.println(packetSize);
// only print packet data for non-RTR packets
while (CAN.available()) {
Serial.print((char)CAN.read());
}
Serial.println();
}
Serial.println();
}
}
So I will greatly appreciate help from anybody, because I have struggled with this problem for the last 2 weeks and I really want to solve it. Thank you for your time
So I have finally found a solution. I guess the other libraries had some problem sending data, cause I set everything with the same configuration. However, today I found this library->https://www.arduinolibraries.info/libraries/mcp_can, which helped me program correctly the arduino and it works perfectly in connection with another arduino or raspberry pi.

How to read messages from GSM modem in Embedded C?

I am currently working on my mini-project under the domain "Internet of Things". I chose to design a wireless Notice board using a GSM module.
I divided the project into two modules. First, the Arduino-LED board interface which perfectly completed.
Second, GSM-Arduino interface. Basically, a message/SMS will be sent from the mobile phone to the GSM module and then we have to read that message from GSM module using Arduino. I am facing a problem here. The message is being sent to the GSM modem but I am not able to read it. I tried writing different codes, but its not working. The message is not being displayed.
Here is the code snippet I tried.
`#include SoftwareSerial.h
SoftwareSerial SIM900A(2,3);// RX | TX
// Connect the SIM900A TX to Arduino pin 2 RX
// Connect the SIM900A RX to Arduino pin 3 TX.
void setup()
{
SIM900A.begin(9600); // Setting the baud rate of GSM Module
Serial.begin(9600); // Setting the baud rate of Serial Monitor(Arduino)
Serial.println ("SIM900A Ready");
delay(100);
Serial.println (" Press s to send and r to recieve ");
}
void loop()
{
if (Serial.available()>0)
switch(Serial.read())
{
case 's': SendMessage();
break;
case 'r': RecieveMessage();
break;
}
if (SIM900A.available()>0)
Serial.write(SIM900A.read());
}
void SendMessage()
{
SIM900A.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
Serial.println ("Set SMS Number");
SIM900A.println("AT+CMGS=\"+91xxxxxxxxxx\"\r"); //Replace with your mobileno.
delay(1000);
Serial.println ("Set SMS Content");
SIM900A.println("Hello, I am SIM900A GSM Module");// The SMS text you want to send
delay(100);
Serial.println ("Finish");
SIM900A.println((char)26);// ASCII code of CTRL+Z
delay(1000);
Serial.println (" ->SMS Sent");
}
void RecieveMessage()
{
Serial.println ("SIM900A Receiving SMS");
delay (1000);
SIM900A.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
delay(1000);
Serial.write (" ->Unread SMS Recieved");
}`
You might have to set the preferred SMS storage to SIM card using the command:
SIM900A.print("AT+CPMS=\"SM\"\r");
Also, move this command to setup():
SIM900A.print("AT+CMGF=1\r");
Lastly, note how I have used SIM900A.print() instead of SIM900A.println() and send a '\r' or 0x0d after each command. println() sends a "\n\r" and that causes problems in some modems.

Arduino Digital Input Causing Output Problems

I am working on a simple project that involves reading a 12-bit binary encoder signal and cycles a digital pin high and low (depending on the angular location of the encoder).
My problem is that when the digital output pin is told to go low, it only goes down to about 4V. I also noticed that it would occasionally dip down to ground, instead of just 4V. Much to my surprise, when I unplug the Arduino, the digital output pin is still reading 4V. When I turned off power to my encoder which is connected up the the Arduino through 12 digital input pins, the output pin dropped down to 0V. When I turn the encoder back on it goes back up to about 4V.
I have tried connecting the output pin to ground and obviously the voltage goes to 0, but as soon as I remove the connection to ground the voltage springs back up to around 4V. It appears as though somehow the voltage being fed to the digital input pins (in the form of a ~5V digital input signal) is preventing the digital output pin from going to ground. I have no idea why this is the case, I have searched all over and could not find any similar complaints. My code is posed below, any help is greatly appreciated!
unsigned long CamAngle = 0; // Variable to store encoder value
unsigned long PrevCamAngle = 0; // Variable to store previous encoder value
int i = 0; // Cycle index
int BDC[] = {683,2048,3413}; // BDC angle (12-bit encoder value)
int TDC[] = {0,1365,2731}; // TDC angle (12-bit encoder value)
boolean TDCycle = false; // TDC Cycle
boolean BDCycle = false; // BDC Cycle
boolean Cycle = true; // Encoder rollover cycle (replaces TDC cycle 0)
void setup()
{
pinMode(37, INPUT); // Encoder bit 0 (PORTC-0)
pinMode(36, INPUT); // Encoder bit 1 (PORTC-1)
pinMode(35, INPUT); // Encoder bit 2 (PORTC-2)
pinMode(34, INPUT); // Encoder bit 3 (PORTC-3)
pinMode(22, INPUT); // Encoder bit 4 (PORTA-0)
pinMode(23, INPUT); // Encoder bit 5 (PORTA-1)
pinMode(24, INPUT); // Encoder bit 6 (PORTA-2)
pinMode(25, INPUT); // Encoder bit 7 (PORTA-3)
pinMode(26, INPUT); // Encoder bit 8 (PORTA-4)
pinMode(27, INPUT); // Encoder bit 9 (PORTA-5)
pinMode(28, INPUT); // Encoder bit 10 (PORTA-6)
pinMode(29, INPUT); // Encoder bit 11 (PORTA-7)
pinMode(30, OUTPUT); // Set PORTC pin 8 to output (stop pin float)
pinMode(31, OUTPUT); // Set PORTC pin 7 to output (stop pin float)
pinMode(32, OUTPUT); // Set PORTC pin 6 to output (stop pin float)
pinMode(33, OUTPUT); // Set PORTC pin 5 to output (stop pin float)
digitalWrite(30, LOW); // Set PORTC pin 8 low (stop pin float)
digitalWrite(31, LOW); // Set PORTC pin 7 low (stop pin float)
digitalWrite(32, LOW); // Set PORTC pin 6 low (stop pin float)
digitalWrite(33, LOW); // Set PORTC pin 5 low (stop pin float)
pinMode(53, OUTPUT); // Set pin 53 (PORTB-0) as digital output
digitalWrite(53, LOW); // Set pin 53 (PORTB-0) LOW
}
void loop()
{
// Cam Angle Update
PrevCamAngle = CamAngle; // Set variable to previous encoder value
CamAngle = (PINA << 4) + PINC; // Read encoder, set variable to value
if (TDCycle == true && CamAngle >= TDC[i]) // Wait for encoder to reach angle if cycle active
{
PORTB = 0; // Set digital pin 53 LOW
TDCycle = false;
BDCycle = true;
}
if (BDCycle == true && CamAngle >= BDC[i]) // Wait for encoder to reach angle if cycle active
{
PORTB = 1; // Set digital pin 53 HIGH
TDCycle = false;
BDCycle = true;
i++;
if (i > 2) // Reset every 3 cycles (3 cycles per revolution)
{
i = 0;
BDCycle = false;
Cycle = true;
}
}
if (Cycle == true && CamAngle < (PrevCamAngle + 1)) // Wait for encoder to cycle back to 0
{
PORTB = 0; // Set digital pin 53 LOW
BDCycle = true;
Cycle = false;
}
}
As you can see I am utilizing direct port manipulation, however I have the same issues when I use the Arduino library commands as well.
As it turned out, the issue was the internal protection diodes in the Arduino. All the pins have internal protection diodes connected to ground and the 5V rail. So when power is disconnected, the 5V rail drops down to ground (or it should), however if you have power coming in from the IO pins, current will start to flow in through the protection diodes connected to the 5V rail and begin to power your Arduino.
This looks like it is a question better suited for https://arduino.stackexchange.com/ or https://electronics.stackexchange.com/. However, based on the info that you've given, my first thought is that this is not so much a coding issue as it is a problem with your connections - particularly when you can see the issue with no power applied to the Arduino.
Take a look at the datasheet for your encoder and make sure that you are not connecting an output to the output of the Arduino. There may also be a requirement for an external pull-down resistor. Finally, try a different pin - I have had Arduino pins that have died from exceeding the current ratings (whoops!).
Hope that helps!

Arduino2max digital pin communication to max using an Arduino mega 2560

Im working on connecting an Arduino Mega 2560 into max msp, I have adapted the Arduino2max arduino code and max patch.
I have adapted the max patch and succeeded with all 16 analog inputs from arduino into max but cannot get any digital pins over number 13 into max msp. I was wondering if anyone had had any sucsess with this?
Any help and comments would be greatly appreciated!
Many thanks
Joe
here is the arduino code adapted from Arduino2max v.5 which can be found here http://www.arduino.cc/playground/Interfacing/MaxMSP
int x = 0;
int ledpin = 13;
void setup ()
{
// 115200 is the default Arduino Bluetooth speed
Serial.begin(115200);
///startup blink
digitalWrite(13,HIGH);
delay(600);
digitalWrite(13,LOW);
pinMode(13,INPUT);
}
void loop()
{
// Check serial buffer for characters
if (Serial.available() > 0){
if (1){ //Serial.read() == 'r') { // If an 'r' is received then read the pins
// Read and send analog pins 0-15
for (int pin= 0; pin<=15; pin++)
{
x = analogRead(pin);
sendValue (x);
}
// Read and send digital pins 2-53
for (int pin= 2; pin<=53; pin++)
{
x = digitalRead(pin);
sendValue (x);
}
// Send a carriage return to mark end of pin data.
Serial.println();
// add a delay to prevent crashing/overloading of the serial port
delay (5);
}
}
}
// function to send the pin value followed by a "space".
void sendValue (int x){
Serial.print(x);
Serial.print(32, BYTE);
}
Thanks again!
I suggest you to use the OSC Protocol to communicate between the Arduino Mega and Max.
I use the library ardosc. There is no documentation on it but its not really hard to use it and it is a good library.
If you cannot use it do not hesitate to ask me some explanations

Reading Virtual Serial Port with MicroC for 8051

I have a problem , please help me.
for about a project homework ı need read from virtual serial port with microC and send this info to AT89C52 microconttoller..
This is my source code:
int uart_rd;
void main() {
P1=0X00;
UART1_Init(9600);
delay_ms(100);
while(1)
{
if(UART1_Data_Ready()){
uart_rd=UART1_read();
if(uart_rd=='1')
{P1=0X01; delay_ms(1500); P1=0X00; }
if(uart_rd=='2')
{P1=0X02; delay_ms(1500); P1=0X00; }
}
}
}
BUT I cant get info from the port. Where is the mistake.Please help me...
You are defining your UART receive variable (uart_rd) as an int, which is a 2 byte variable. I would expect UART1_read() to return a single byte (char).
I am not familiar with your particular setup or debugging/troubleshooting options, but you might try writing some code to assist in debugging your issue. The following example may be useful. It does assume that LEDs are connected to both port 1 and port 2, so some adjustment may be necessary.
char uart_rd;
void main()
{
UART1_Init(9600); // Initialize UART at 9600 bps
delay_ms(100); // Wait for UART to stabilize
while(1)
{
if(UART1_Data_Ready())
{
P2 = 0xFF; // Turn ON PORT2 LEDs upon data ready
uart_rd = UART1_read(); // Receive data
P1 = uart_rd; // Display data on port 1 LEDs
UART1_write(uart_rd); // Transmit same data back
delay_ms(1500); // Brief delay
P1 = 0x00; // Turn OFF port 1 LEDs
P2 = 0x00; // Turn OFF port 2 LEDs
}
}
}

Resources