Sure! Here’s an example Arduino Uno code to send an SMS using a GSM SIM900 module:
C++ #include// Configure software serial port SoftwareSerial mySerial(10, 11); // RX, TX void setup() { // Open serial communication with PC Serial.begin(9600); Serial.println("Initializing..."); // Set the baud rate and wait for SIM900 module to be ready mySerial.begin(9600); delay(1000); Serial.println("SIM900 ready"); // Set SMS mode to text mySerial.println("AT+CMGF=1"); delay(1000); // Set phone number to send SMS to mySerial.println("AT+CMGS=\"+1234567890\""); // replace with your phone number delay(1000); // Send SMS message mySerial.println("Hello from Arduino!"); delay(1000); // End SMS message with Ctrl+Z mySerial.write(26); delay(1000); Serial.println("SMS sent"); } void loop() { // Do nothing }
In this code, we start by creating a `SoftwareSerial` object called `mySerial` that will be used to communicate with the SIM900 module. We then set up the `Serial` object to communicate with the PC for debugging purposes.
In the `setup()` function, we first initialize the `mySerial` object with a baud rate of 9600 and wait for the SIM900 module to be ready. We then use AT commands to set the SMS mode to text and specify the phone number to send the SMS to. Note that you should replace the phone number with your own phone number.
After setting up the SMS parameters, we use the `mySerial.println()` method to send the SMS message “Hello from Arduino!” and wait for 1 second. We then use the `mySerial.write()` method to send the Ctrl+Z character (ASCII code 26) to end the SMS message.
Finally, we print out a message to the PC indicating that the SMS was sent.
In the `loop()` function, we do nothing because we only need to send the SMS once.
Note that you may need to modify the AT commands and/or the baud rate depending on the specific SIM900 module you are using and the network you are connecting to. Also, make sure to connect the SIM900 module to the correct pins on the Arduino Uno and provide it with sufficient power (e.g. using a 9V battery or external power supply).