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.
Related
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);
}
Trying to run arduino code through pyserial,
Trying to run a code which communicates to the arduino to turn an LED on/off and stays in that HIGH/LOW state until otherwise communicated via the serial port again
the problem I am having is as soon as the python code has run the LED turns off and I can't figure out why its doing this.. any help would be much appreciated.
Arduino CODE
int data;
int EM=13;
#define ever
void setup() {
Serial.begin(9600); //initialize serial COM at 9600 baudrate
pinMode(EM, OUTPUT); //declare the LED pin (13) as output
digitalWrite (EM, LOW); //Turn OFF the Led in the beginning
Serial.println("Actuating Electromagnet");
}
void loop() {
if (Serial.available()>0) //whatever the data that is coming in serially and assigning the value to the variable “data”
{
data = Serial.read();
Serial.println(data);
}
if (data == '1')
digitalWrite (EM, HIGH); //Turn On the Led
else if (data == '0')
digitalWrite (EM, LOW); //Turn OFF the Led
PYTHON SCRIPT
import serial
import time
ArduinoUnoSerial = serial.Serial('/dev/cu.usbmodem1411',9600)
time.sleep(2)
print ArduinoUnoSerial.readline()
ArduinoUnoSerial.write('1')
Here i commented the pinmode section:
void setup() {
//pinMode(10, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(10, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
in my test is equal to:
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(10, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(10, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
it only seems that the LED blinks weakly.
Thanks for your time.
From the documentation for digitalWrite():
Write a HIGH or a LOW value to a digital pin.
If the pin has been configured as an OUTPUT with pinMode(), its
voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V
boards) for HIGH, 0V (ground) for LOW.
If the pin is configured as an INPUT, digitalWrite() will enable
(HIGH) or disable (LOW) the internal pullup on the input pin. It is
recommended to set the pinMode() to INPUT_PULLUP to enable the
internal pull-up resistor. See the digital pins tutorial for more
information.
If you do not set the pinMode() to OUTPUT, and connect an LED to a
pin, when calling digitalWrite(HIGH), the LED may appear dim. Without
explicitly setting pinMode(), digitalWrite() will have enabled the
internal pull-up resistor, which acts like a large current-limiting
resistor.
I have a pixicam connected to an Arduino Uno. The pixi.init() method seems to be interfering with a normal blinky program. Here is the smallest reproducable program:
#include<SPI.h>
#include<Pixy.h>
Pixy pixy;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
pixy.init();
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
If I comment out the pixi.init() line then the program works as normal (blinking on and off). When the line is not commented out, the light fails to blink.
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.