stm32 pin assignment in Arduino IDE - arduino

I have a multi-cooker which is not working so I decided to use its PCB for my project. It has an STM32f103c8t7 chip on it. some LEDs and a buzzer and a display, which I may not need though. I have found the connection between chip pins and elements on the board (for example the buzzer is connected to the pin number 13, "PA3") but I have a problem. since this is my first try with stm32 in arduino, I don't know how to name the pins of chip. I have tried PA3, 13 and D13 but none of them seem to to useful. what should I do to make the board buzz?
the buzzer is ok and works fine. Actually one of the buzzer's pin is connected to 12 V and another pin is connected to a transistor which its base is connected to the chip.
this is the code that I have tried:
void setup() {
pinMode(PA3, OUTPUT);
// pinMode(13, OUTPUT);
// pinMode(D13, OUTPUT);
}
void loop() {
digitalWrite(PA3, HIGH);
delay(delayTime);
digitalWrite(PA3, LOW);
delay(delayTime);
// digitalWrite(13, HIGH);
// delay(delayTime);
// digitalWrite(13, LOW);
// delay(delayTime);
// digitalWrite(D13, HIGH);
// delay(delayTime);
// digitalWrite(D13, LOW);
// delay(delayTime);
}

Related

Program only works when connected to PC

new to arduino programming, making a proximity sensor with an HC-SRO4 module. It works almost flawlessly when connected to my computer via USB, but whenever I connect it to the wall, it stops working. What I've noticed is that the "TX" light also isn't on when it's connected to the wall, but it is when connected to the PC. The adapter i'm using when it's plugged into the wall is 9V/1A DC that came with the arduino.
void setup() {
#define LED 8
#define trigPin 12
#define echoPin 13
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1
;if (distance >= 60 || distance <= 0){
Serial.println("no object detected");
digitalWrite(LED, LOW);}
else {
Serial.println("object detected");
digitalWrite(LED, HIGH);
}}
There is a bug where the serial command suche as serial.begin, serial.write and serial.read if they are meant to for debug purposes on your cpu have a blocking effect on your program. I've experienced it once or twice so try commenting those lines out.

Arduino fails with Serial.println

I've got an Arduino which works with the following basic example for blinking:
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
But if I add Serial.println it doesn't blink and doesn't output anything to the Serial moniter:
void setup() {
pinMode(13, OUTPUT);
Serial.begin(115200);
}
void loop() {
Serial.println("Loop");
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
What am I doing wrong?
You're not doing anything wrong you may have a bad chip, or your baud rate too high, try 9600
Also can you give me details on your chip, some chips don't have serial.
If you're using the same chip as the leonardo you may need this:
Serial.begin(9600);
while (!Serial) {} //Wait for serial port to connect
When you change the baud rate in Serial.begin(115200) the receiving terminal should have the same baud rate or you will see nothing there.

How to make arduino go in two direction using h-bridge?

I am tring to build a line follower robot, i have an h-bridge and arduino to control the direction and speed.
I burnt this code to my arduino board, but the robot just go forward and never go on the oppsite direction:
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;
void setup()
{
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void demoOne()
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 200);
// turn on motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enB, 200);
delay(2000);
// now change motor directions
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
// now turn off motors
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop()
{
demoOne();
delay(1000);
demoTwo();
delay(1000);
}
How to adjust the code to make it go for both directions?
UPDATE:
I am using h bridge l298n and four dc motors "2 right and 2 left", powering it with two 9v batteries and powering the arduino uno with 5v which i get from the h bridge based on this article:
https://hackerstore.nl/PDFs/Tutorial298.pdf
enA, in1, and in2 for the two right motors
enB, in3, and in4 for the two left motors
Your code looks perfectly fine, make sure your hardware is working properly and that everything is connected properly, also make sure that you are not using the 5V from your Arduino to power the motors since the small voltage regulator on the Arduino board is not capable of handling high currents such as 500mA (Unless you replace it with a bigger regulator and give it a big heatsink), if you are powering the arduino and motors from USB make sure your USB port can handle the power required by the two motors, oh and make sure pins 10 and 5 are PWM pins
Also using a dedicated motor library may help to simplify your code, I wrote you one here:
#include <Arduino.h>
#include <stdbool.h>
#define enA 10
#define enB 5
#define i1 8
#define i2 9
#define i3 6
#define i4 7
void initMotors(){
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(i1, OUTPUT);
pinMode(i2, OUTPUT);
pinMode(i3, OUTPUT);
pinMode(i4, OUTPUT);
}
void motor1Stop(){
digitalWrite(i1, LOW);
digitalWrite(i2, LOW);
}
void motor2Stop(){
digitalWrite(i3, LOW);
digitalWrite(i4, LOW);
}
void motorsEnabled(bool en){
digitalWrite(enA, en);
digitalWrite(enB, en);
}
void enableMotors(){
motorsEnabled(HIGH);
}
void motor1Forward(){
digitalWrite(i1, HIGH);
digitalWrite(i2, LOW);
}
void motor1Backward(){
digitalWrite(i1, LOW);
digitalWrite(i2, HIGH);
}
void motor2Forward(){
digitalWrite(i3, LOW);
digitalWrite(i4, HIGH);
}
void motor2Backward(){
digitalWrite(i3, HIGH);
digitalWrite(i4, LOW);
}
To include this just make a new tab and name it whatever you want (as long as there are no spaces and it ends in .h), then add #include "name.h" to the beginning of your code, this will help you to reduce future bugs
For speed control you can add this:
void setMotorSpeeds(int motor1, int motor2){
analogWrite(enA, motor1);
analogWrite(enB, motor2);
}
Your code is fine, so you will need to identify the problem by "divide and conquer"
Connect a single motor to each channel, and forget the arduino part for now.
Connect the L298 power supply. If you read the instructions you will see that you can use the integrated 5V power regulator, or you can supply an external 5V. If using a different power supply for 5V and Motors make sure you connect both GND together otherwise it won't work.
Make sure you have the driver properly connected to power supply, take two cables and use them to connect in1 to GND, in2 to 5V and enA to 5V (without the arduino, just manually connect them with a cable). One of the motors should turn one side.
Now connect in1 to 5V and in2 to GND (keeping enA connected to 5V), the motor should turn to the other side.
If this does not happen then you are not properly connecting the L298 or it is damaged.
If it works then the problem comes from your arduino. Connect 3 LEDs, one in the pin9, one in the pin 8 and one in the pin 10 (which are the pins you connect to in1 and in2 and enA).
Run your program and you should see the LEDS turn on and off accordingly.
If not, your arduino has a problem in some pins (maybe you short circuited them?)
If both experiments work there is absolutely no reason for them to not work together, so start small, divide your problem into pieces and identify it.
Good luck.

TCS3200 collor sensor with arduino r3 uno

I am trying to wire up the TCS3200 color sensor for arduino uno R3 and I am having issues. I have checked many documents on the web and they all have different ways of wiring. Some document set OE pin on TCS3200 to ground on the Arduino, and other don't use the OE pin. Some document set s0= Arduino pin 2, s1= Arduino pin 3, s2= Arduino pin 4, s3=Arduino pin 5, and out pin to Arduino pin 10. Other documents have a different pin wiring configurations.
Hence i was wonder what is the correct way to wire TCS3200 color sensor to the Arduino? I have tried a lot of wiring configuration but the LEDs on TCS3200 won't light up. Does anyone have a solution?
Here is my code:
void setup() {
pinMode(2, OUTPUT); //s0
pinMode(3, OUTPUT); //s1
pinMode(4, OUTPUT); //s2
pinMode(5, OUTPUT); //s3
pinMode(10, INPUT); //out
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
Serial.begin(9600);
}
void loop() {
digitalWrite(4,LOW);
digitalWrite(5,LOW);
Serial.println("red");
Serial.print(pulseIn(10, LOW));
Serial.println();
delay(100);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
Serial.println("green");
Serial.print(pulseIn(10, LOW));
Serial.println();
delay(100);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
Serial.println("blue");
Serial.print(pulseIn(10, LOW));
Serial.println();
delay(100);
}
Thanks

Arduino Ultra Sonic Sensor always returns 0

I am doing a basic project in Arduino UNO connecting an Ultra Sonic sensor (HC-SR04) which should print in the serial monitor the distance of the closest object but it always print 0.
This is my code:
long distance;
long time;
void setup(){
Serial.begin(9600);
pinMode(4, OUTPUT);
pinMode(2, INPUT);
}
void loop(){
digitalWrite(2,LOW);
delayMicroseconds(5);
digitalWrite(2, HIGH);
delayMicroseconds(10);
time = pulseIn(4, HIGH);
distance = int(0.017*time);
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm.");
delay(1000);
}
And this is the breadboard:
The primary issue that I see is that your code doesn't match your wiring diagram.
For example, your diagram shows Trig connected to pin 4. The Trig should be the output from your Arduino but you have it defined as an input.
The Echo is connected to pin 2 and it should be an input, but you have it defined as an output.
Finally, in your loop(), you are not even using pin 2 or pin 4, but pins 9 and 8. Another issue is the timing you use in setting the trigger pulse - it does not match the datasheet. I would do something like this (assuming that you are actually connected to the pins shown in your diagram):
#define sensorTrigPin 4
#define sensorEchoPin 2
void setup()
{
Serial.begin(9600);
pinMode(sensorTrigPin, OUTPUT);
pinMode(sensorEchoPin, INPUT);
}
void loop()
{
int pulseWidth = 0;
digitalWrite(sensorTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(sensorTrigPin, LOW);
pulseWidth = pulseIn(sensorEchoPin, HIGH);
Serial.print("Pulse Width: ");
Serial.print(pulseWidth);
delay(1000);
}
Note that pulseWidth is just the amount of time that it takes from the beginning of the Echo pulse going high to the end of the same pulse (when it goes low). You would still have to calculate the distance based on the value of pulseWidth.
UPDATE BASED ON RECENT EDIT TO THE QUESTION
If you change a portion of your loop() code to this, it should work:
void loop(){
digitalWrite(4, HIGH); //was (2, LOW)
delayMicroseconds(10); //was (5)
digitalWrite(4, LOW); //was (2, HIGH)
//REMOVED EXTRA DELAY
time = pulseIn(2, HIGH); //was (4,HIGH);
... //Keep the rest of your code the same.
}
Try connecting your VCC of the sensor to 3V3 instead of 5V. This might sound odd, but I tried it and it worked well. Also, please make sure that your echo and trig pin match the code.

Resources